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?
Looks like your
EntryandEntryTableInfoclasses are managed objects (subclasses ofNSManagedObject).Managed objects should ‘live’ only inside of a managed object context (
NSManagedObjectContextobject). Particularly they should be created only by and within such context.Your
[Entry create]method probably does so, but[[EntryTableInfo alloc] init]creates a managed object with no context, which is wrong.Try looking at
[Entry create]method implementation code and creating aEntryTableInfoinstance in a similar way.