ios - Magical Record 添加对象,不同上下文错误

标签 ios core-data magicalrecord

我在我的应用中使用 Magical Record,并希望为用户添加添加“Note”的功能,它是“entry”的子项。

我添加了这段代码:

    [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',但我仍然遇到同样的错误。

我错过了什么?

最佳答案

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

[self.entry addNotesObject: newNote];

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

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

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

关于ios - Magical Record 添加对象,不同上下文错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19165704/

相关文章:

ios - 如何在Xcode 6.3.1中为iPhone 4/5添加模拟器

ios - Magical Record 级联删除(truncate All)不删除

ios - 在 subview 按钮中调用父函数

ios - 与 OpenGL ES + CoreData 的并发

ios - 什么类型返回executeFetchRequest

ios - 不能使用谓词来比较 Magical Record 中的日期

ios - 将 JSON 数据转换并存储到 iOS Core Data

ios - 为什么核心数据不会持久保存到磁盘?

ios - 将事件的 RecurrenceRule 保存到 CoreData

ios - 科洛达委托(delegate) : fatal error: unexpectedly found nil while unwrapping an Optional value