Ios Coredata查找一个包含值的数组
我有一个核心数据表,它包含100个数据,一个可变数组包含一些数据。用于搜索可变数组的iw ant是否包含表中的数据。可变数组包含3个参数使用这些参数,我需要找到一个id。该id包含表中的数据 我正在使用这个代码Ios Coredata查找一个包含值的数组,ios,objective-c,xcode,core-data,nspredicate,Ios,Objective C,Xcode,Core Data,Nspredicate,我有一个核心数据表,它包含100个数据,一个可变数组包含一些数据。用于搜索可变数组的iw ant是否包含表中的数据。可变数组包含3个参数使用这些参数,我需要找到一个id。该id包含表中的数据 我正在使用这个代码 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ IN self.@allKeys" ,self.beaconListArray ]; request.predicate = predicate; NSError
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ IN self.@allKeys" ,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");
}
据我所知,您拥有一系列数据。每个数据项都是一个字典、一个自定义对象或其他东西,并且有一个唯一的ID。您希望将数据插入或更新到核心数据中 我不确定您正在为uniqueId使用什么属性,或者您正在尝试插入什么类型的数据。所以这段代码只是为了演示 首先从数据中获取uniqueid。比如:
NSArray* thingIdArray = [self.beaconListArray valueForKeyPath:@"uuid"];
下一步做一个提取。您的谓词可以类似于
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K IN %@", @"uuid", thingIdArray];
接下来,将结果(托管对象的数组)转换为字典,其中键是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]
}
我有一个可变数组{major=1;minor=4541;uuid=“xxxxxxxx”},{major=1;minor=6000;uuid=“xxxxxxx”},使用这个数据我想在coredata datatable中搜索并找出相应的id。使用这个id我需要在视图中显示一些东西。如何得到这些