RestKit:在注销时重置持久存储后映射了 0 个对象
Posted
技术标签:
【中文标题】RestKit:在注销时重置持久存储后映射了 0 个对象【英文标题】:RestKit: 0 objects mapped after resetting persistent stores on logout 【发布时间】:2015-01-27 09:35:24 【问题描述】:在 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】:看起来我把问题复杂化了。这是一个适合我的解决方案:
// 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];
澄清一下:根本不需要做任何事情。
【讨论】:
在那之后你还设置所有的 RestKit 值吗?我有相同的代码,但我从 RestKit 收到一个错误,告诉我它无法保存我的新登录用户的数据:Failed saving managed object context to the persistent store
。以上是关于RestKit:在注销时重置持久存储后映射了 0 个对象的主要内容,如果未能解决你的问题,请参考以下文章
RestKit CoreData 0.20.3 - 映射完成后保存 MOC 之前的额外检查