相关疑难解决方法(0)

如何将NSManagedObject从一个上下文复制或移动到另一个上下文?

我有一个相当标准的设置,一个从未保存的暂存器MOC(包含从Web下载的一堆对象)和另一个永久MOC持久化对象.当用户从scratchMOC中选择一个对象添加到她的库时,我想要1)从scratchMOC中删除对象并插入到permanentMOC中,或者2)将对象复制到permanentMOC中.该核心数据FAQ说,我可以复制的对象是这样的:

NSManagedObjectID *objectID = [managedObject objectID];
NSManagedObject *copy = [context2 objectWithID:objectID];
Run Code Online (Sandbox Code Playgroud)

(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 …

cocoa cocoa-touch core-data nsmanagedobject nsmanagedobjectcontext

53
推荐指数
3
解决办法
2万
查看次数