2017-04-20 79 views
0

我有一個核心數據表,其中包含100個數據和一個可變數組有一些數據。 IW螞蟻搜索可變數組包含數據存在於表或not.mutable陣列包含3個PARAMS使用這些PARAMS我需要找出。那包含i中的表 一個id正在使用此代碼Coredata發現一個數組包含值

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ IN [email protected]" ,self.beaconListArray ]; 
request.predicate = predicate; 
NSError *error = nil; 
NSArray *objs = [managedObjectContext executeFetchRequest:request error:&error]; 
if (error) { 
    [NSException raise:@"no find" format:@"%@", [error localizedDescription]]; 
    NSLog(@"there is noooo with same id exsist"); 
} 
if (objs.count > 0) { 
    NSLog(@"there is a with same id exsist. Use update method"); 
}else { 
    NSLog(@"there's no with same id. Use insert method"); 
} 

回答

0

作爲我知道你有一組數據。每個數據項都是字典或自定義對象或其他東西,並具有唯一標識。您想要將數據插入或更新到核心數據中。

我不確定您正在使用uniqueId的屬性或您嘗試插入的數據類型。所以這段代碼只是爲了演示。

首先從數據中獲取uniqueIds。類似於:

NSArray* thingIdArray = [self.beaconListArray valueForKeyPath:@"uuid"]; 

接下來執行提取操作。您的謂詞可能看起來像

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K IN %@", @"uuid", thingIdArray]; 

next將託管對象數組的結果轉換爲字典,其中的關鍵字是uniqueId。

NSMutableDictionary* dict = [NSMutableDictionary dictionary]; 
for (CoreDataThing* t in result) { 
    NSString* thingId = t.uuid; 
    if (thingId) { 
     dict[thingId] = t; 
    } 
} 

現在對每個數據項可以看到,如果該項目已經存在

for (NSDictionary* a in self.beaconListArray) { 
    NSString* thingId = a[@"uuid"]; 
    CoreDataThing* coreDataThing = dict[thingId]; 
    if (!coreDataThing) { 
     coreDataThing = //create it here and set default values 
    } 
    [coreDataThing updateWithSomeObject:a] 
} 
+0

我有一個可變數組 { 主要= 1; minor = 4541; uuid =「xxxxxxxx」; }, { major = 1; minor = 6000; uuid =「xxxxxxx」; }, 使用此數據我想在coredata數據表中搜索並找出相應的id.using該id我需要在我的視圖中顯示的東西。 如何獲取這些 – Manju

相關問題