如何将 NSManagedObject 从一个上下文复制或移动到另一个上下文? [英] How do I copy or move an NSManagedObject from one context to another?

查看:35
本文介绍了如何将 NSManagedObject 从一个上下文复制或移动到另一个上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一个相当标准的设置,有一个永远不会保存的暂存MOC(包含一堆从网络下载的对象)和另一个永久的MOC,它可以保留对象.当用户从scratchMOC 中选择一个对象添加到她的库中时,我想1)从scratchMOC 中删除该对象并插入到permanentMOC 中,或者2)将该对象复制到permanentMOC 中.Core Data FAQ 说我可以像这样复制一个对象:

I have what I assume is a fairly standard setup, with one scratchpad MOC which is never saved (containing a bunch of objects downloaded from the web) and another permanent MOC which persists objects. When the user selects an object from scratchMOC to add to her library, I want to either 1) remove the object from scratchMOC and insert into permanentMOC, or 2) copy the object into permanentMOC. The Core Data FAQ says I can copy an object like this:

NSManagedObjectID *objectID = [managedObject objectID];
NSManagedObject *copy = [context2 objectWithID:objectID];

(在这种情况下,context2 将是永久MOC.)但是,当我这样做时,复制的对象出错了;数据最初未解析.当它得到解决后,所有的值都为零;来自原始托管对象的任何数据(属性或关系)都没有被实际复制或引用.因此,我看不出使用这个 objectWithID: 方法和使用 insertNewObjectForEntityForName: 将一个全新的对象插入到 PermanentMOC 中有何区别.

(In this case, context2 would be permanentMOC.) However, when I do this, the copied object is faulted; the data is initially unresolved. When it does get resolved, later, all of the values are nil; none of the data (attributes or relationships) from the original managedObject are actually copied or referenced. Therefore I can't see any difference between using this objectWithID: method and just inserting an entirely new object into permanentMOC using insertNewObjectForEntityForName:.

我意识到我可以在 PermanentMOC 中创建一个新对象并从旧对象手动复制每个键值对,但我对这个解决方案不太满意.(我有许多不同的托管对象,我遇到了这个问题,所以我不想在继续开发时为所有这些对象编写和更新 copy: 方法.)有更好的方法吗?

I realize I can create a new object in permanentMOC and manually copy each key-value pair from the old object, but I'm not very happy with that solution. (I have a number of different managed objects for which I have this problem, so I don't want to have to write and update copy: methods for all of them as I continue developing.) Is there a better way?

推荐答案

首先,在单个线程上拥有多个 NSManagedObjectContext 不是标准配置.99% 的情况下,您只需要一个上下文即可解决这种情况.

First, having more than one NSManagedObjectContext on a single thread is not a standard configuration. 99% of the time you only need one context and that will solve this situation for you.

为什么你觉得你需要不止一个 NSManagedObjectContext?

Why do you feel you need more than one NSManagedObjectContext?

这实际上是我见过的少数几个有意义的用例之一.为此,您需要将对象从一个上下文递归复制到另一个上下文.工作流程如下:

That is actually one of the few use cases that I have seen where that makes sense. To do this, you need to do a recursive copy of the object from one context to the other. The workflow would be as follows:

  1. 在持久上下文中创建新对象
  2. 从源对象获取属性字典(使用 -dictionaryWithValuesForKeys-[NSEntityDescription attributesByName] 来做到这一点.
  3. 将值字典设置到目标对象上(使用-setValuesForKeysWithDictionary)
  4. 如果您有关系,则需要递归地进行此复制,并通过硬编码(以避免某些循环逻辑)或使用-[NSEntityDescription relationshipByName]
  1. Create new object in persistent context
  2. get a dictionary of the attributes from the source object (use -dictionaryWithValuesForKeys and -[NSEntityDescription attributesByName] to do this.
  3. set the dictionary of values onto the target object (using -setValuesForKeysWithDictionary)
  4. If you have relationships, you will need to do this copy recursively and walk the relationships either hard coded (to avoid some circular logic) or by using the -[NSEntityDescription relationshipsByName]

正如另一个人提到的,你可以从我的书中下载示例代码 The Pragmatic Programmers核心数据手册并查看此问题的一种解决方案.当然在书中我更深入地讨论它:)

As mentioned by another, you can download the sample code from my book from The Pragmatic Programmers Core Data Book and see one solution to this problem. Of course in the book I discuss it more in depth :)

这篇关于如何将 NSManagedObject 从一个上下文复制或移动到另一个上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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