Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy an object between 2 NSManagedObjectContext

How can I copy an NSManagedObject from one NSManagedObjectContext to another?

I have 2 different NSManagedObjectContext, each with it's own NSManagedObjectModel and separate persistent store. Each of the 2 MOC has an entity called Observation that is tied to the same Observation class.

I want to copy objects from one MOC to the other. Since the objects are of the same class, I would like to just fetch an object from one MOC and save it to the other. Is this possible, or do I need to insert a new object into the target MOC and then set each property one at a time?

like image 894
Jeff Wolski Avatar asked Jul 18 '26 01:07

Jeff Wolski


1 Answers

You need to copy one object and insert a new one into the other managed object context. Most likely you would have to copy the property values over one by one to do this.

The reason is that all these objects have opaque unique ID, so you cannot just take it from one context to the other.

like image 117
Mundi Avatar answered Jul 19 '26 15:07

Mundi