我正在从一个给我 JSON 数据的 API 导入数据。添加到核心数据中的每个对象都有一个名为“id”的属性。我希望能够在初始导入后检查 id 是否已经在核心数据中。到目前为止,我已经为它编写了这段代码:
- (void)mergeData: (NSString *)entityDescription {
NSPredicate *pred = [NSPredicate predicateWithFormat:@"id == %@", _bank.id]; // Bank is the entity
// id is the key from the raw JSON
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
NSError *error = nil;
[fetch setEntity:[NSEntityDescription entityForName:@"Banks" inManagedObjectContext:self.managedObjectContext]];
[fetch setPredicate:pred];
NSArray *items = [self.managedObjectContext executeFetchRequest:fetch error:&error];
for (NSManagedObject *object in items) {
// Loop over all the items and check to see if the ids match with any ids from the feed
// if any of them don't match, add the new ids
if (!match) {
// add new object
// I'm not sure how to implement this part
}
}
但我知道它并不完整,我不确定如何实现这部分的其余代码。任何帮助,将不胜感激。