RestKit:在注销时重置持久存储后映射了 0 个对象

RestKit: 0 objects mapped after resetting persistent stores on logout

在 RestKit 版本 0.23.x 上,我尝试在用户注销时使用以下方法重置核心数据的持久存储:

// 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] 

但是,当我重新登录时,RestKit 无法映射响应:

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]

重新启动应用程序后,restkit 再次正确映射对象:

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]

据我所知,我正在重置和清除相关的托管对象上下文和获取的结果控制器:

- (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;
}

我错过了什么?

更多信息 - 使用跟踪日志记录级别,我可以看到服务器正在正确返回数据,但它没有被映射。 (出于安全原因,数据被截断):

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 --> }
}

另一个编辑: 该问题似乎(如预期的那样)仅影响实体映射,而不影响普通对象映射(在登录步骤中有一个这样的成功映射)。

看来我把问题复杂化了。这是一个现在对我有用的解决方案:

// 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];

澄清一下:根本不需要任何东西。