RestKit Object Mapping: difficulty using setObject

2019-09-08 07:37发布

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?

2条回答
冷血范
2楼-- · 2019-09-08 08:00

I looked at your updated description. Give this a try:

  1. Switch back to the setObjectMapping:forResourcePathPattern:withFetchRequestBlock
  2. 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.

查看更多
霸刀☆藐视天下
3楼-- · 2019-09-08 08:14

Two things to check out to determine why you are seeing the described behavior:

  1. 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.
  2. 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:

  1. GET /channels returns 2xx status code with new payload
  2. RKManagedObjectLoader performs pruning
查看更多
登录 后发表回答