ローカルコアデータデータベースをリモートJSONAPIと同期しようとしています。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でクリアするにはどうすればよいですか?どうも