How to set a relationship from one NSManageObject to another in a different NSManagedObjectContext

岁酱吖の 提交于 2019-12-12 22:42:14

问题


Because my app is multi-threaded I use two NSManagedObjectContexts. The main context, that runs in the main thread and another context that only runs in a separate thread.

I have created a small test app. It has two Core Data Entities. Parent and Child. Parent has a one-to-many relationship to Child. Child has a 1-1 relationship to Parent.

In one test method (that runs in a separate thread) I get a Parent instance, that has been created during a run time before. So it's in the main context. I get this parent with this line of code:

Parent *tmpParent = [[parentController selectedObjects] objectAtIndex:0];

Then I create some children in the thread-context (managedObjectContextInBackground), set their parent to the tmpParent, give them a name and save the thread-context:

Child *child1 = (Child *)[NSEntityDescription insertNewObjectForEntityForName:@"Child" inManagedObjectContext:managedObjectContextInBackground];
[child1 setName:@"Homer"];
[child1 setParent:tmpParent];

Child *child2 = (Child *)[NSEntityDescription insertNewObjectForEntityForName:@"Child" inManagedObjectContext:managedObjectContextInBackground];
[child2 setName:@"Wilma"];
[child2 setParent:tmpParent];

[self saveManagedObjectContextInBackground];

If I execute that method the application crashes and says: Illegal attempt to establish a relationship 'parent' between objects in different contexts

That's why I added this line below the tmpParent declaration:

[managedObjectContextInBackground insertObject:tmpParent];

But, the application crashes again saying: An NSManagedObject may only be in (or observed by) a single NSManagedObjectContext

I looked through the documentation, but I couldn't find a way to solve this problem.

Question: How can I set the relationship of Child to Parent, when Parent is in a different NSManagedObjectContext, than Child ??


回答1:


Your object can be loaded from another context, thus, when you create a new NSManagedObject in your other thread, nothing stops you from fetching an existing NSManagedObject which may or may not be being used in the main thread, and use that reference in order to set up your relationship.

When you exit the thread and merge the contexts, this change will get updated for the object that was until this point happily existing in memory on the main thread.



来源:https://stackoverflow.com/questions/8346455/how-to-set-a-relationship-from-one-nsmanageobject-to-another-in-a-different-nsma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!