2

我有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;
}
4

2 回答 2

3

添加新项目后,您必须在项目类别和代码中添加此方法调用此方法,您将在其中传递居住在核心数据中的数组对象,它会删除数组中没有的所有对象

+(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];
            }
        }
    }

}
于 2013-10-14T10:48:38.790 回答
2

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

或者,从一个有效的 API 的角度来看,服务器应该给你一个删除列表,因为如果你向它发送一个“最后请求日期”,它就不需要确认没有改变的项目......

于 2013-10-14T10:40:03.563 回答