gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:09:21 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com