问题
My setup
The following all works fine:
RKManagedObjectMapping* chanMapping = [RKManagedObjectMapping mappingForClass:[Channel class] inManagedObjectStore:objectStore];
chanMapping.primaryKeyAttribute = @"chanId";
[chanMapping mapKeyPathsToAttributes:
@"id",@"chanId",
@"name", @"chanName",
nil];
[objectManager.mappingProvider setMapping:chanMapping forKeyPath:@"Channels.channel"];
I can call
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/channels" delegate:self];
and I get my channel's from the server and they're stored locally by Core Data. Perfect.
The issue
However, I now wan't to have RestKit automatically delete Channels from the Core Data store that have been removed from the server the next time a GET is performed. I understand this is supported by adding the following:
[objectManager.mappingProvider setObjectMapping:chanMapping forResourcePathPattern:@"/channels" withFetchRequestBlock:^ (NSString *resourcePath) {
return [Channel fetchRequest];
}];
However with this all the Channels get deleted whenever there is anything new on the server.
Things I've tried [UPDATED]
I've debugged using the steps in the answer below. It looks as though the mapping isn't working / is not being found (i.e. I haven't properly associated the mapping with the resource path).
In deleteCachedObjectsMissingFromResult
the cachedObjects
array looks good, has all the objects that should be there from the last time but the results
array is empty which obviously results in [results containsObject:object]
always being NO
and all the objects being deleted.
Do I need to change something to do with the resource path mapping?
回答1:
I looked at your updated description. Give this a try:
- Switch back to the
setObjectMapping:forResourcePathPattern:withFetchRequestBlock
- Set the
rootKeyPath
on the object mapping you register to Channels.channel
Then give it another try. There is some API work in progress to provide URL and keyPath based mapping configuration in a single line, but its not merged to development yet.
回答2:
Two things to check out to determine why you are seeing the described behavior:
- Open up RKManagedObjectLoader and put a breakpoint within
isResponseMappable
. This method checks if the response was loaded from the cache and performs a load of the objects using the objects returned by the managed object cache if it returns YES. This is probably where you are seeing the return of the cached objects from. - As for the deletion of cached objects, put a breakpoint within
deleteCachedObjectsMissingFromResult
and see what is going on in there (if you are even making it into the routine).
The scenario to expect automatic pruning would be:
- GET /channels returns 2xx status code with new payload
- RKManagedObjectLoader performs pruning
来源:https://stackoverflow.com/questions/10516987/restkit-object-mapping-difficulty-using-setobjectmappingforresourcepathpattern