CoreData + restkit +创建新实体

时间:2014-06-10 22:36:28

标签: ios core-data restkit-0.20

我正在使用 RestKit Coredata 并从服务器获取数据并显示。

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

我找到了实现这一点的正确方法,并遇到了两个要点。

  1. MOC不应跨线程共享
  2. 在没有保存的情况下,在MOC中创建的对象在另一个线程中不可用。
  3. 但我认为,由于记录从服务器响应更新,它不再找到orig对象。我只是不知道正确的解决方案是什么。

    这是我的代码 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。所以肯定有些事我搞砸了。

    欣赏任何见解!

1 个答案:

答案 0 :(得分:3)

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

,而不是致电if (![self.managedObjectContext saveToPersistentStore:&error]) {
相关问题