Ios 核心数据重复条目

Ios 核心数据重复条目,ios,objective-c,core-data,Ios,Objective C,Core Data,我从一个提供JSON数据的API导入数据。添加到核心数据中的每个对象都有一个名为“id”的属性。我希望能够在初始导入后检查该id是否已经存在于核心数据中。到目前为止,我已经为此编写了以下代码: - (void)mergeData: (NSString *)entityDescription { NSPredicate *pred = [NSPredicate predicateWithFormat:@"id == %@", _bank.id]; // Bank i

我从一个提供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            
            }

        }

但我知道它还不完整,我也不知道如何实现这部分的其余代码。如果您有任何帮助,我们将不胜感激。

请查看,特别是“高效查找或创建”部分,这正是您所需要的。谢谢!我去看看。