开发者

How to make a shallow copy of NSManagedObject when objects' contexts differ?

开发者 https://www.devze.com 2023-03-26 07:56 出处:网络
I am implementing \"duplicate\" functionality in my iOS application. I\'m using the following workflow:

I am implementing "duplicate" functionality in my iOS application. I'm using the following workflow:

  1. present a list of managed objects in initial context in root view controller
  2. when user taps on a row, create a new context pass it to "detail" view controller with duplicated managed object ([[DetailController alloc] initWithObject:clonedObject inContext:newContext]).

However I am struggling with the concept of reassigning relations from source object to the cloned one since their managed object contexts differ. What would be correct app开发者_开发技巧roach to this:

  1. Should I just reassign the pointer value and do not bother about MOC or...
  2. I should refetch the values in new context depending on their unique identifiers?
  3. Any other option I did not think of?

P.S. Contexts are using same persistent store coordinator.


Managed object IDs are thread safe. As such, you can pass a managed object ID to the MOC in your view controller, retrieve that object via existingObjectWithID:error, then perform the duplication in that context. This way, the objects never cross MOC boundaries.

0

精彩评论

暂无评论...
验证码 换一张
取 消