I've implemented a simple REST request in a structure as follows:
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
operation.targetObject = nil;
operation.savesToPersistentStore = YES;
operation.managedObjectContext = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
operation.managedObjectCache = [RKManagedObjectStore defaultStore].managedObjectCache;
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
// Handle success
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
// Handle failure
}];
How can I modify the code so that:
- in case of success I need the related entity on DB to be emptied and its content replaced with the new one arrived from the WS (no update, entire content replaced);
- in case of failure nothing should happen and the entity on DB must remain as it is.
In order to delete the DB entity, before the operation I would do as reported here: Core Data: Quickest way to delete all instances of an entity, but that would empty the entity no matter what the WS answer is.
Any help is really appreciated, Thanks a lot, DAN
EDIT1:
As suggested by @Wain, I've added a fetch request block to the operation like this:
RKFetchRequestBlock fetchRequestBlock = ^NSFetchRequest *(NSURL *URL)
{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"EntityToBeUpdated"];
fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"order" ascending:NO] ];
return fetchRequest;
};
operation.fetchRequestBlocks = @[fetchRequestBlock];
operation.deletesOrphanedObjects = YES;
but the result doesn't change: the items on db are not updated or deleted.