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

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

问题描述

我假设这是一个相当标准的设置,一个从不保存(包含一堆从网络下载的对象)的临时保存MOC和另一个持久的对象的永久MOC。当用户从scratchMOC选择一个对象添加到她的库时,我想要么1)从scratchMOC中删除对象并插入permanentMOC,或2)将对象复制到permanentMOC。 核心数据常见问题表示我可以复制这样的对象:

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会是permanentMOC。)但是,是故障;数据最初未解决。当它得到解决,以后,所有的值都是nil;实际上没有复制或引用来自原始managedObject的数据(属性或关系)。因此,我不能看到使用这个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 relationshipsByName]

  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天全站免登陆