根据JSON删除核心数据中的对象

时间:2013-10-14 10:26:06

标签: ios objective-c json uitableview core-data

我有NSManagedObjectModel个实体,“项目”。我将所有项目都显示在UITableView NSFetchedResult控制器中。现在,如果 JSON 有新项目我插入它们,如果JSON有更新项目,我更新核心数据上下文中的项目。

所以,我的问题是当我得到一个 JSON ,其项目少于上下文。我想过两种方法来删除我的上下文中的项目。一种方法是删除所有上下文并使用新项目再次保存。另一种方法是创建一个包含上下文中所有项目的数组,并通过id检查 JSON 中的项目,如果没有项目,则将其删除。

我有这个想法,但我不知道哪种方式最好。我也想过backgroundContext

我现在使用这种方法而没有删除方法:

#pragma mark - Project List service

- (void)getProjectListWithCpompletionBlock:(CompletionBlock)completionBlock{
    NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:kAPIProjectList parameters:nil];
    [request setTimeoutInterval:kTimeOutRequest];

    AFJSONRequestOperation *requestOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        NSDictionary *projects = [JSON valueForKey:kTagProjects];

        for (NSDictionary *projectDic in projects) {
            Project *pro = [Project getProjectWithId: [projectDic objectForKey:kTagProjectId] ];
            if (pro) {
                [Project updateProjectWithDictionary:projectDic];
                NSLog(@"update %@ ",[projectDic objectForKey:kTagProjectId]);
            } else {
                [Project createProjectWithDictionary: projectDic];
                NSLog(@"create %@ ",[projectDic objectForKey:kTagProjectId]);
            }

        }
            [ypCoreDataManager  saveContext];
            if (completionBlock) {
                completionBlock(NO, nil);
            }
        }
        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *aError, id JSON) {
           NSLog(@"%@ Failure JSNON Error%@", NSStringFromSelector(_cmd), aError);
            if (completionBlock) {
                completionBlock(YES, aError);
            }
        }];

    [self enqueueHTTPRequestOperation:requestOperation];
}

Project + Helper是我的项目类别,这里是代码。

+ (Project *)createProjectWithDictionary:(NSDictionary *)dic {

    Project *project = nil;

    project = [NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:mainContext];

    project.projectId = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectId] intValue]];
    project.title = [[dic valueForKey:kTagProjectTitle]description];
    project.estimatedPrice = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectEstimatedPrice] floatValue]];
    NSMutableArray *tags = [[NSMutableArray alloc] init];
    tags = [dic objectForKey:kTagProjectsTags];

    NSMutableSet *tagSet = [[NSMutableSet alloc]init];
    for (NSDictionary * tagDic in tags){
        NSString *tagName = [tagDic objectForKey:kTagProjectTagName];
        Tag *tag = [Tag insertTagName:tagName inManagedObjectContext:mainContext];
        [tagSet addObject:tag];
    }
    [project addTags:tagSet];



    return  project;
}


// Return project by id

+ (Project *)getProjectWithId:(NSString *) projectId {

    Project *project = nil;

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Project"];
    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"projectId" ascending:YES]];
    request.predicate = [NSPredicate predicateWithFormat:@"projectId = %@", [projectId description]];

    // Execute the fetch

    NSError *error = nil;
    NSArray *matches = [mainContext executeFetchRequest:request error:&error];


    if (!matches || ([matches count] > 1)) {  // nil means fetch failed; more than one impossible (unique!)
        // handle error

    } else { // found the Project, just return it from the list of matches (which there will only be one of)
        project = [matches lastObject];
    }

 return  project;
}

//   Update project

+ (Project *)updateProjectWithDictionary:(NSDictionary *)dic {
    Project *project = nil;

    // Build a fetch request to see if we can find this Project in the database.

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Project"];
    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]];
    request.predicate = [NSPredicate predicateWithFormat:@"projectId = %@", [dic[kTagProjectId]description]];

    // Execute the fetch

    NSError *error = nil;
    NSArray *matches = [mainContext executeFetchRequest:request error:&error];

    // Check what happened in the fetch

    if (!matches || ([matches count] > 1)) {  // nil means fetch failed; more than one impossible (unique!)
        // handle error
    } else {
        project = [matches lastObject];
        project.projectId = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectId] intValue]];
        project.title = [[dic valueForKey:kTagProjectTitle]description];
        project.estimatedPrice = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectEstimatedPrice] floatValue]];
        NSMutableArray *tags = [[NSMutableArray alloc] init];
        tags = [dic objectForKey:kTagProjectsTags];

        NSMutableSet *tagSet = [[NSMutableSet alloc]init];
        for (NSDictionary * tagDic in tags){
            NSString *tagName = [tagDic objectForKey:kTagProjectTagName];
            Tag *tag = [Tag insertTagName:tagName inManagedObjectContext:mainContext];
            [tagSet addObject:tag];
        }
        [project addTags:tagSet];

    }

    return project;
}

2 个答案:

答案 0 :(得分:3)

您必须在项目的类别和代码中添加此方法,然后添加新项目调用此方法,其中您传递生活在Core Data中的数组对象,并删除所有未包含的对象数组

+(void)removeExpiredProjectBy:(NSMutableArray *)ProjectLiving inContext:(NSManagedObjectContext *)context{


    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Project"];

    if (projectLiving.count) {

        request.predicate = [NSPredicate predicateWithFormat:@"NOT (projectId IN %@)", [projectLiving copy]];

        NSError *error = nil;
        NSArray *matches = [context executeFetchRequest:request error:&error];
        if (matches.count != 0) {
            for (Project *pro in matches) {
                [context deleteObject:pro];
            }
        }
    }

}

答案 1 :(得分:2)

在处理JSON时,您可以构建已添加/更新的所有ID的列表。然后,在完成之后,您可以使用谓词创建一个获取请求,该谓词查找NOT (id IN %@)所有项目并提供ID列表。这将仅返回需要删除的项目。

或者,从有效的API角度来看,服务器应该为您提供删除列表,因为如果您发送“最后请求日期”,则无需确认未更改的项目...