0

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?

Michael Behan
  • 3,433
  • 2
  • 28
  • 38
  • 1
    just a confirmation... you asking how to automatically remove deleted objects the next time you perform a GET, or are you asking how to implement a DELETE call? – shawnwall May 11 '12 at 14:37
  • Automatically remove deleted objects the next time I perform a GET. Will edit question to clarify. – Michael Behan May 11 '12 at 14:47
  • I think I ended up at the same commit that you did in RestKit trying to figure this out. Yet another example of poor documentation in RestKit, sorry but I can't figure out more than you did. You could always loop through the loaded objects in the delegate callback and manually delete those that aren't found... :\ – shawnwall May 11 '12 at 16:36
  • yeah, already have that coded up but really don't want to use it if it's not necessary. – Michael Behan May 14 '12 at 07:38

2 Answers2

2

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.

Blake Watters
  • 6,607
  • 1
  • 43
  • 33
1

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
Blake Watters
  • 6,607
  • 1
  • 43
  • 33
  • Updated the question with more details based on the suggested debugging (and corrected the description of the behavior I'm getting), I'm still missing something. – Michael Behan May 16 '12 at 10:34