我有NSManagedObjectModel
一个实体“项目”。我让所有项目在UITableView
带有NSFetchedResult
控制器的情况下显示它们。现在,如果JSON有新项目,我会插入它们,如果 JSON 更新了项目,我会更新 Core Data 上下文中的项目。
所以,我的问题是当我得到一个比上下文少项目的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;
}