1

我正在尝试将我的本地核心数据数据库与远程 JSON API 同步。我正在使用 RestKit 将 JSON 值映射到本地托管对象。这是一段代码:

- (IBAction)testButtonPressed:(id)sender {

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
NSError *error = nil;
BOOL success = RKEnsureDirectoryExistsAtPath(RKApplicationDataDirectory(), &error);
if (! success) {
    RKLogError(@"Failed to create Application Data Directory at path '%@': %@", RKApplicationDataDirectory(), error);
}

// - - - - - - - - Change the path !
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"AC.sqlite"];
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path
                                                                 fromSeedDatabaseAtPath:nil
                                                                      withConfiguration:nil
                                                                                options:nil
                                                                                  error:&error];
if (! persistentStore) {
    RKLogError(@"Failed adding persistent store at path '%@': %@", path, error);
}
[managedObjectStore createManagedObjectContexts];

// - - - - - - - - Here we change keys and values
RKEntityMapping *placeMapping = [RKEntityMapping mappingForEntityForName:@"Place"
                                                    inManagedObjectStore:managedObjectStore];
[placeMapping addAttributeMappingsFromDictionary:@{
    @"place_id": @"place_id",
    @"place_title": @"place_title",
    @"site": @"site",
    @"address": @"address",
    @"phone": @"phone",
    @"urating": @"urating",
    @"worktime": @"worktime",
    @"lat": @"lat",
    @"lng": @"lng",
    @"about": @"about",
    @"discount": @"discount",
    @"subcategory_title": @"subcategory_title",
    @"subcategory_id": @"subcategory_id",
    @"category_title": @"category_title",
    @"image_url": @"image_url"}];


//RKEntityMapping *articleMapping = [RKEntityMapping mappingForEntityForName:@"Article" inManagedObjectStore:managedObjectStore];
//[articleMapping addAttributeMappingsFromArray:@[@"title", @"author", @"body"]];
//[articleMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"categories" toKeyPath:@"categories" withMapping:categoryMapping]];


NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
// here we need to change too
RKResponseDescriptor *responseDescriptor =
    [RKResponseDescriptor responseDescriptorWithMapping:placeMapping
                                            pathPattern:nil // @"/articles/:articleID"
                                                keyPath:@"data.place_list"
                                            statusCodes:statusCodes];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://allocentral.api.v1.ladybirdapps.com/place/?access_token=19f2a8d8f31d0649ea19d478e96f9f89b&category_id=1&limit=10"]];

RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request
                                                                                  responseDescriptors:@[responseDescriptor]];

operation.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
operation.managedObjectCache = managedObjectStore.managedObjectCache;

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
    NSLog(@" successfull mapping ");
    [self refreshContent];

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Failed with error: %@", [error localizedDescription]);
}];

NSOperationQueue *operationQueue = [NSOperationQueue new];
[operationQueue addOperation:operation];
}

- (void) refreshContent {
//  perform fetch
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
//  reload data
[self.tableView reloadData];
}

它运行完美,可以获取所有对象并将它们存储在核心数据中,但是如果在服务器上删除了某些对象,并且它们不在 JSON 响应中,它们就会留在 detebase 中。我怎样才能让restkit清除不在响应中的对象?谢谢

4

1 回答 1

1

每当您从服务器收到新的 JSON 响应时,您应该照常处理它,将新条目添加到您的核心数据对象中。

然后遍历您的 Core Data 对象,并检查它们是否包含在 JSON 中(使用对您的对象有意义的任何方法),如果没有,则删除它们。

或者,如果您通过 JSON 传递某种 ID,您可以在将对象添加到 Core Data 的同时将每个 ID 存储在 NSArray 中。然后对与数组中的 ID 不匹配的任何 Core Data 对象进行谓词搜索,然后将其删除。

哪个更好取决于您是否有更多新/现有项目或更多要删除的项目。

于 2013-03-02T22:18:46.393 回答