我在我的应用中使用 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/