1

I have two entities A and B. A has an to-many relationships to B. A and B have different managedObjectContext contextA and contextB. I need to add managed object B to managed object A after modifying some attributes of B in contextB, but it is impossible because they are not in the same context. So how could i pass changes in contextB to contextA without save it to persistentStore? ps. if u wanna know why i need two different context u could run over this link undo all changes made in a view controller.

Community
  • 1
  • 1
lu yuan
  • 7,207
  • 9
  • 44
  • 78

1 Answers1

-1

Copy a managed object from one context to another, you can use the object’s object ID, as illustrated in the following example.

NSManagedObjectID *objectID = [managedObject objectID];

NSManagedObject *copy = [contextB objectWithID:objectID];

After copying you can add the object B to object A.

Hope this help you.

iamsult
  • 1,581
  • 1
  • 15
  • 21
  • Actually there is already a object with the same objectID in context2. So [context2 objectWithID:objectID] won't work. Unless i delete the object in context2 first. – lu yuan May 03 '12 at 07:27
  • No need to delete the object you can replace the old object with the copied object. – iamsult May 03 '12 at 07:42
  • It doesn't work, as there is already an object B in context A. It could not update the object B in context A with the object B in context B. – lu yuan May 03 '12 at 07:51