Search code examples
objective-ccore-datarestkit

RestKit: 0 objects mapped after resetting persistent stores on logout


On RestKit version 0.23.x, I'm trying to reset core data's persistent store when the user logs out, using the following:

// Cancel all requests
[[RKObjectManager sharedManager] cancelAllObjectRequestOperationsWithMethod:RKRequestMethodAny matchingPathPattern:@"/"];

[[RKObjectManager sharedManager].operationQueue cancelAllOperations];

[RKObjectManager sharedManager].managedObjectStore.managedObjectCache = nil;

// Clear our object manager
[RKObjectManager setSharedManager:nil];

// Reset our persistent store
[[RKManagedObjectStore defaultStore] resetPersistentStores:nil];

// This command runs the reconfiguration of RestKit, the same
// code that is applied when the app launches.
[self setup] 

However, when I log back in, RestKit fails to map responses:

2015-01-27 10:28:16.452 <APP_NAME>[80341:12445388] I restkit.network:RKObjectRequestOperation.m:220 GET '<api url>' (200 OK / 0 objects) [request=0.2098s mapping=0.0011s total=0.2112s]

Once I restart the app, restkit correctly maps objects again:

2015-01-27 10:33:13.918 <APP_NAME>[80400:12458200] I restkit.network:RKObjectRequestOperation.m:220 GET '<api url>' (200 OK / 1 objects) [request=0.3969s mapping=0.0332s total=0.4305s]

As far as I can tell, I'm resetting and nil'ing the relevant managed object context and fetched results controller:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    // These are lazily instantiated, so will be
    // recreated the next time they are referenced
    _fetchedResultsController = nil;
    _managedObjectContext = nil;
}

What am I missing?

More info - with trace logging level, I can see that the server is correctly returning data, but it's not being mapped. (Data is truncated for security reasons):

2015-01-27 17:32:03.773 <APP_NAME>[86020:12883038] T     restkit.network:RKObjectRequestOperation.m:218 GET '<api url>' (200 OK / 0 objects) [request=0.3788s mapping=0.0011s total=0.3802s]:

response.headers={
  Connection = "keep-alive";
  "Content-Length" = 342;
  "Content-Type" = "application/json; charset=utf-8";
  Date = "Tue, 27 Jan 2015 16:32:03 GMT";
  Etag = "\"-968344033\"";
  Vary = "Accept-Encoding";
  "X-Powered-By" = Express;
}
response.body={
  "status": "success",
  "data": { <--- truncated exactly one object --> }
}

Another edit: The problem seems to affect (as would be expected) only entity mappings, not plain object mappings (there is one such successful mapping in the login step).


Solution

  • Looks like I was overcomplicating the problem. Here's a solution that works for me now:

    // Cancel any network operations and clear the cache
    [[RKObjectManager sharedManager].operationQueue cancelAllOperations];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
    // Cancel any object mapping in the response mapping queue
    [[RKObjectRequestOperation responseMappingQueue] cancelAllOperations];
    
    // Reset persistent stores
    [self.managedObjectStore resetPersistentStores:nil];
    

    To clarify: No need to be nilling anything at all.