在受管对象上下文之间传递对象 [英] Passing objects between managed object contexts

查看:109
本文介绍了在受管对象上下文之间传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iphone应用程序与2管理对象上下文。我的一个上下文处理选择器,它允许用户添加新记录,然后选择这些新记录之一。一旦选择器被隐藏,该被管理对象上下文被保存并被丢弃。

I have an iphone app with 2 managed object contexts. One of my contexts deals with a picker, which allows the user to add new records and then select one of those new records. Once the picker is hidden, that managed object context is saved and discarded.

然后我想在我的第二个管理对象上下文中使用这个选择的对象,并将它添加(关联)到另一个对象。该第二MOC的改变可以被保存或删除。这就是为什么创建第一个MOC以允许在选择器中进行的更改始终保存,无论他们保存还是丢弃第二个MOC中的更改。

I then want to use this selected object in my second managed object context, and add (relate) it to another object. This second MOC's changes may be saved or deleted. This is why the first MOC is created to allow the changes made in the picker to always be saved no matter wether they save or discard the changes in the second MOC.

I希望这是清楚!我的问题是,当用户从选择器选择对象时,此对象驻留在不同的上下文中需要使用它。有什么方法可以在上下文之间传递对象?

I hope this is clear! My problem is that when the user selects the object from the picker, this object resides in a different context to where it needs to be used. Is there a way you can pass objects between contexts? Perhaps using the object's ID (after it has been persisted).

感谢您的帮助!

推荐答案

您在问题中提到了正确的解决方案。您不能在多个上下文之间传递NSManagedObjects,但您可以传递NSManagedObjectID并使用它们查询由该ID表示的对象的适当上下文。因此,简单地保留数据(通过保存:),然后将ID传递给其他上下文,并使用它来请求适当对象的上下文。

You mentioned the correct solution in the question. You cannot pass NSManagedObjects between multiple contexts, but you can pass NSManagedObjectIDs and use them to query the appropriate context for the object represented by that ID. So simply persist out the data (via a save:), and then pass the ID to the other context and use it to ask the context for the appropriate object.

在你想做什么你可能想要装备mergeChangesFromContextDidSaveNotification:使一个上下文中的更改自动反映在另一个。

Depending on what you want to do you may want to rig up the mergeChangesFromContextDidSaveNotification: so that changes in one context are automatically reflected in the other.

这篇关于在受管对象上下文之间传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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