First, having more than one
NSManagedObjectContext on a single thread is not a standard configuration. 99% of the time you only need one context and that will solve this situation for you.
Why do you feel you need more than one
That is actually one of the few use cases that I have seen where that makes sense. To do this, you need to do a recursive copy of the object from one context to the other. The workflow would be as follows:
- Create new object in persistent context
- get a dictionary of the attributes from the source object (use
-[NSEntityDescription attributesByName]to do this.
- set the dictionary of values onto the target object (using
- If you have relationships, you will need to do this copy recursively and walk the relationships either hard coded (to avoid some circular logic) or by using the
As mentioned by another, you can download the sample code from my book from The Pragmatic Programmers Core Data Book and see one solution to this problem. Of course in the book I discuss it more in depth 🙂