如何在不同的NSManagedObjectContext中设置从一个NSManageObject到另一个的关系 [英] How to set a relationship from one NSManageObject to another in a different NSManagedObjectContext

查看:112
本文介绍了如何在不同的NSManagedObjectContext中设置从一个NSManageObject到另一个的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我的应用程序是多线程的,我使用两个NSManagedObjectContexts。主上下文在主线程中运行,另一个上下文只在单独的线程中运行。



我创建了一个小测试应用程序。它有两个核心数据实体。父母和孩子。父对象与孩子具有一对多关系。孩子与父有一个1-1关系。



在一个测试方法(运行在一个单独的线程)中,我得到一个父实例,时间之前。所以它在主上下文。我得到这个父代这行代码:

 父* tmpParent = [[parentController selectedObjects] objectAtIndex:0];然后,我在线程上下文( managedObjectContextInBackground )中创建一些子对象,然后在线程上下文中创建一些子对象。 

将它们的父级设置为tmpParent,为其赋予一个名称并保存线程上下文:

  Child * child1 = [NSEntityDescription insertNewObjectForEntityForName:@ChildinManagedObjectContext:managedObjectContextInBackground]; 
[child1 setName:@Homer];
[child1 setParent:tmpParent];

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

[self saveManagedObjectContextInBackground];

如果我执行该方法,应用程序崩溃并说:
在不同上下文中的对象之间建立一个关系父。



这就是为什么我在tmpParent声明下面添加这一行:

  [managedObjectContextInBackground insertObject:tmpParent]; 

但是,应用程序再次崩溃说:
NSManagedObject只能(或观察)单个NSManagedObjectContext



我查看了文档,但我找不到一种方法来解决这个问题。

问题:当Parent在不同的NSManagedObjectContext中时,如何设置Child与Parent的关系,而不是Child?

解决方案

您的对象可以从另一个上下文中加载,因此,当您在其他线程中创建一个新的NSManagedObject时,没有什么能阻止您获取现有的NSManagedObject



当您退出线程并合并上下文时,此更改将在主线程中使用,并使用该引用为了设置您的关系。将被更新的对象,直到这一点高兴地存在于主线程的内存中。


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 ??

解决方案

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.

这篇关于如何在不同的NSManagedObjectContext中设置从一个NSManageObject到另一个的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆