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

时间:2015-01-27 09:35:25

标签: objective-c core-data restkit

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

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

1 个答案:

答案 0 :(得分:0)

看起来我的问题太复杂了。这是一个适合我的解决方案:

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

澄清:根本不需要任何费用。