【问题标题】:Magical Record add object, different context errorMagical Record添加对象,不同的上下文错误
【发布时间】:2013-10-03 17:42:05
【问题描述】:

我在我的应用程序中使用 Magical Record,并希望为用户添加添加“注释”的功能,它是“条目”的子级。

我添加了这段代码:

    [MagicalRecord saveWithBlock: ^(NSManagedObjectContext *localContext) {
        Note *newNote = [Note MR_createInContext: localContext];

        newNote.content = noteContent;
        newNote.name = @"User Note";

        [self.entry addNotesObject: newNote];
     }
                      completion: ^(BOOL success, NSError *error) {
                          if (error != nil)
                          {
                              // show alert
                          }
                          else if (success)
                          {
                              [[self tableView] reloadData];
                          }
                      }];

我在最后一行一直遇到的错误是“非法尝试在不同上下文中的对象之间建立关系'条目'”

我尝试将 'entry' 和 'newNote' 的上下文设置为 'localContext',但仍然遇到相同的错误。

我错过了什么?

【问题讨论】:

    标签: ios core-data magicalrecord


    【解决方案1】:

    self.entry 是在不同的上下文中创建的,因此您无法从该上下文中访问它。 而不是:

    [self.entry addNotesObject: newNote];
    

    你应该首先在localContext中找到self.entry对象:

    [[self.entry MR_inContext:localContext] addNotesObject: newNote];
    

    您可以在Performing Core Data operations on Threads 找到在并发环境中使用 MagicalRecord 的说明。虽然很短,但我认为即使您不直接使用 CD,也值得阅读Core Data Programming Guide

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      相关资源
      最近更新 更多