我创建一个托管对象保存上下文并发布如下:
[[RKObjectManager sharedManager] postObject:tag mapResponseWith:tagMappingForPOST delegate:tagLoader];
tagLoader
获取对象,但无法保存在RestKit的上下文中:
Failed to save managed object context after mapping completed: The operation couldn’t be completed. (Cocoa error 134030.)
NSUnderlyingException=Cannot update object that was never inserted.
我正在使用同一个类的同一个后端服务器(Parse.com)做同样的事情并且它工作正常。关于为什么会出现“无法更新从未插入的对象”的潜在原因的任何线索。错误?
答案 0 :(得分:3)
事实证明,RestKit希望在向postObject:mapResponseWith:delegate
发送RKObjectManager
消息之前保存上下文。我的问题是我在postObject:mapResponseWith:delegate
消息之后立即保存了上下文(我认为这是正常的,因为这仍然是在加载程序从Web服务得到响应之前。我正在这样做:
NSManagedObject *myObj = [NSEntityDescription insertNewObjectForEntityForName:@"MyObjects" inManagedObjectContext:context];
[[RKObjectManager sharedManager] postObject:myObj mapResponseWith:objMappingForPOST delegate:myObjLoader];
[context save:&error];
...然后myObjLoader
会收到回复并尝试更新myObj
(createdAt,parse的objectId等)中的属性,并抛出一条错误消息,指出myObj不存在。
我应该仔细阅读RestKit代码来确认,但我很确定发生了什么RKObjectManager
正在postObject:mapResponseWith:delegate
创建后台线程和来自托管对象库的上下文postObject:mapResponseWith:delegate
1}}消息被接收,并且永远不会在响应返回之前与可能存在的任何潜在合并进行更新。说实话,我希望在收到回复后创建上下文。
所以要做的是在发送NSManagedObject *myObj = [NSEntityDescription insertNewObjectForEntityForName:@"MyObjects" inManagedObjectContext:context];
[context save:&error];
[[RKObjectManager sharedManager] postObject:myObj mapResponseWith:objMappingForPOST delegate:myObjLoader];
消息之前保存上下文,如下所示:
{{1}}