1

I have this code:

Entry *entry = [Entry create];

NSOrderedSet *media = [[NSOrderedSet alloc] init];
entry.media = media;

EntryTableInfo *entryTableInfo = [[EntryTableInfo alloc] init];
entry.entryTableInfo = entryTableInfo;

[[CoreDataStore mainStore] save];

And when it saves, it's giving me the error Illegal attempt to establish a relationship between objects in different contexts.

The NSOrderedSet code was already there, and was working fine. The EntryTableInfo code wasn't, that's new, and that's the cause of the crash, but I'm not sure why?

4

1 回答 1

3

看起来您的EntryEntryTableInfo类是托管对象(的子类NSManagedObject)。

托管对象应该仅在托管对象上下文(NSManagedObjectContext对象)内“存在”。特别是它们应该只在这样的上下文中创建。

您的[Entry create]方法可能会这样做,但[[EntryTableInfo alloc] init]会创建一个没有上下文的托管对象,这是错误的。

尝试查看方法实现代码并以类似方式[Entry create]创建实例。EntryTableInfo

于 2012-04-27T17:38:40.523 回答