ios - CoreData + Restkit + 创建新实体

标签 ios core-data restkit-0.20

我将 RestKitCoredata 一起使用,并从服务器获取数据并显示。

现在我正在从客户端发送帖子,并且该对象作为从服务器返回的响应的一部分进行更新。这就是问题开始的地方。

我寻找实现此目的的正确方法,并发现了两个要点。

  1. MOC 不应跨线程共享
  2. 在 MOC 中创建的对象在不保存的情况下在其他线程中不可用。

但我认为由于记录是从服务器响应中更新的,因此它不再找到原始对象。我只是不知道正确的解决办法是什么。

这是我的代码 1.创建本地实体

    NSEntityDescription *itemEntity = [NSEntityDescription entityForName:ENTITY_ITEM inManagedObjectContext:self.managedObjectContext];
    Item *item = [[Item alloc]initWithEntity:itemEntity insertIntoManagedObjectContext:self.managedObjectContext];

// Set params on item here 

// Then save it 
    NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
    DBGLog(@"Tried to save the new item but failed - error %@, %@", error, [error userInfo]);
}

// Then I make the RestKit call to post the item
// The server updates the item Id
[SharedManager postItem:item success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

    // successful case         
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    // failure case         
}];

看起来当它尝试做出响应时没有找到该对象。

我得到了这个异常 -

 *** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x156c87b0 <x-coredata://A42ABF18-01B6-4D78-B81B-62D8B604EB52/Item/p6>''
*** First throw call stack:
(0x2f853f0b 0x39feace7 0x2f5b7fd1 0x2f61a655 0x2f6246a7 0x2f6326e5 0x2f632a95 0x2f63356f 0x3a4d3d3f 0x3a4d86c3 0x2f628e7b 0x2f633271 0x2f5c7f49 0x1c67fb 0x2f62b9cd 0x3a4d9b3b 0x3a4d3d3f 0x3a4d66c3 0x2f81e681 0x2f81cf4d 0x2f787769 0x2f78754b 0x346f46d3 0x320e6891 0x72561 0x3a4e8ab7)
libc++abi.dylib: terminating with uncaught exception of type _NSCoreDataException

如果我不进行“保存”,那么我会在 4S 设备上看到 Cocoa Error 133000。所以肯定是我搞砸了一些事情。

感谢任何见解!

最佳答案

您的评论是正确的,但不是正确的解决方案。问题是您只保存主线程上下文,并且更改不会被推送到父级(持久上下文)。因此,您不应调用 if (![self.managementObjectContext save:&error]) {,而应该调用 if (![self.managementObjectContext saveToPersistentStore:&error]) {

关于ios - CoreData + Restkit + 创建新实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24151976/

相关文章:

ios - 如何从设备制作唯一标识符?

objective-c - IOS CGBitmapContextCreate 大位图

ios - 为什么不鼓励 "didChangeValueForKey:withSetMutation:usingObjects:"覆盖

xcode - 将现有的 xcdatamodeld "folder"添加到 XCode

ios - 将 JSON 对象发布到 Restful Web 服务

iphone - 带有自定义后退按钮的 UINavigationController 子类

iphone - 如何使用 UIScrollView 捕捉缩放?

ios - 如何控制ViewController之间的数据流

ios - 使用 RestKit 发送简单的 GET 请求

ios - 休息套件 0.20 : POST CoreData relationship with Foreign-Key