Eimantas
Eimantas

Reputation: 49354

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

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 approach 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.

Upvotes: 0

Views: 467

Answers (1)

Scott Ahten
Scott Ahten

Reputation: 1171

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.

Upvotes: 1

Related Questions