Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将对象插入核心数据实体时处理重复项_Ios_Objective C_Json_Core Data - Fatal编程技术网

Ios 将对象插入核心数据实体时处理重复项

Ios 将对象插入核心数据实体时处理重复项,ios,objective-c,json,core-data,Ios,Objective C,Json,Core Data,在我的应用程序中,我使用分页从web服务下载数据。输出是一个json字典数组 现在,我将输出json数组保存在核心数据中。所以,我的问题是,每次使用结果数组调用saveInCoreData:方法时,它都会在数据库中创建重复的对象。如果对象已经存在,如何检查对象并更新或替换该对象? myId是一个uniq键 // save in coredata + (void) saveInCoreData:(NSArray *)arr{ // get manageObjectContext AppDeleg

在我的应用程序中,我使用分页从web服务下载数据。输出是一个json字典数组

现在,我将输出json数组保存在核心数据中。所以,我的问题是,每次使用结果数组调用saveInCoreData:方法时,它都会在数据库中创建重复的对象。如果对象已经存在,如何检查对象并更新或替换该对象? myId是一个uniq键

// save in coredata
+ (void) saveInCoreData:(NSArray *)arr{

// get manageObjectContext
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [appDelegate managedObjectContext];

if(arr != nil && arr.count > 0) {

    for(int i=0; i < arr.count; i++){

        SomeEntity *anObj = [NSEntityDescription
                                insertNewObjectForEntityForName:@"SomeEntity"
                                inManagedObjectContext:context];


        anObj.description = [[arr objectAtIndex:i] objectForKey:@"description"];
        anObj.count = [NSNumber numberWithInteger:[[[arr objectAtIndex:i] objectForKey:@"count"] integerValue]];

        // Relationship
        OtherEntity *anOtherObject = [NSEntityDescription
                                   insertNewObjectForEntityForName:@"OtherEntity"
                                   inManagedObjectContext:context];
        creatorDetails.companyName = [[[arrTopics objectAtIndex:i] objectForKey:@"creator"] objectForKey:@"companyName"];
    }
}
//保存在coredata中
+(无效)保存的INCORDEDATA:(NSArray*)arr{
//获取manageObjectContext
AppDelegate*AppDelegate=(AppDelegate*)[[UIApplication sharedApplication]委托];
NSManagedObjectContext*上下文=[appDelegate managedObjectContext];
如果(arr!=nil&&arr.count>0){
对于(int i=0;i
避免重复的最有效方法是获取您已经拥有的所有对象,并避免在迭代结果时处理它们

从结果中获取主题ID:

使用以下主题ID获取现有主题:

获取现有的主题ID:

处理结果:


在处理循环中,试图单独获取每个现有主题在时间上会非常低效。折衷是占用更多内存,但由于您一次只能获取20个对象,这应该是完全没有问题的。

假设您正在添加带有属性、名称、id、拍摄日期、位置等的照片。请选择“id”属性在所有照片中都是唯一的,如果是这种情况,只需在xcdatamodeld和Inspector窗口的“约束”选项卡中选择照片实体,输入该属性名称即可。现在,当您执行“保存”时,核心数据将验证其唯一性,并且您将只获得该属性的一个实例。感谢您提供的详细答案
NSArray *topicIds = [results valueForKeyPath:@"topicId"];
NSFetchRequest *request = ...;
request.predicate = [NSPredicate predicateWithFormat:@"%K IN %@",
                                 @"topicId", topicIds];
NSArray *existingTopics = [context executeFetchRequest:request error:NULL];
NSArray *existingTopicIds = [existingTopics valueForKeyPath:@"topicId"];
for (NSDictionary *topic in results) {
    if ([existingTopicIds containsObject:topic[@"topicId"]]) {
        // Update the existing topic if you want, or just skip.

        continue;
    }

    ...
}