views:

335

answers:

4

I have what I assume is a fairly standard setup, with one scratchpad MOC which is never saved (containing a bunch of objects downloaded from the web) and another permanent MOC which persists objects. When the user selects an object from scratchMOC to add to her library, I want to either 1) remove the object from scratchMOC and insert into permanentMOC, or 2) copy the object into permanentMOC. The Core Data FAQ says I can copy an object like this:

NSManagedObjectID *objectID = [managedObject objectID];
NSManagedObject *copy = [context2 objectWithID:objectID];

(In this case, context2 would be permanentMOC.) However, when I do this, the copied object is faulted; the data is initially unresolved. When it does get resolved, later, all of the values are nil; none of the data (attributes or relationships) from the original managedObject are actually copied or referenced. Therefore I can't see any difference between using this objectWithID: method and just inserting an entirely new object into permanentMOC using insertNewObjectForEntityForName:.

I realize I can create a new object in permanentMOC and manually copy each key-value pair from the old object, but I'm not very happy with that solution. (I have a number of different managed objects for which I have this problem, so I don't want to have to write and update copy: methods for all of them as I continue developing.) Is there a better way?

A: 

You need to make sure that you're saving the context that managedObject lives in. In order to fetch the same object in a different context, it needs to be present in the persistent store.

According to the documentation, objectWithID: always returns an object. So the fact that the fault resolves to an object will all nil values implies that it's not finding your object in the persistent store.

Alex
Just confirmed that this works as advertised--thanks! However, the reason I wanted a second context (scratchMOC) in the first place is actually to avoid saving any of the objects; I want everything to be thrown out when the user exits and reloaded again when the app starts. Guess that may not be possible.
Aeonaut
Take a look at the in-memory store type. It's a store that only lives in memory and goes away when the program exits. Just be careful that you can't have relationships across stores.
Alex
Alex, that looks very tempting, but I'm having a little trouble figuring out how it works. Are the two stores (persistent and in-memory) in two separate contexts or in the same one?
Aeonaut
A: 

The documentation is misleading and incomplete. The objectID methods do not themselves copy objects they simply guarantee that you've gotten the specific object you wanted.

The context2 in the example is in fact the source context and not the destination. You're getting a nil because the destination context has no object with that ID.

Copying managed objects is fairly involved owing to the complexity of the object graph and the way that the context manage the graph. You do have to recreate the copied object in detail in the new context.

Here is some example code that I snipped from the example code for The Pragmatic Programmer's Core Data: Apple's API for Persisting Data on Mac OS X. (You might be able to download the entire project code without buying the book at the Pragmatic site.) It should provide you with a rough idea of how to go about copying an object between context.

You can create some base code that copies objects but the particulars of each object graph's relationships usually mean that you have to customize for each data model.

TechZen
A: 

TechZen thanks for the link. But how can I merge the changes of the cloned object with the original one? Should I use the mergeChangesFromContextDidSaveNotification method?

Jigzat
You should add this as a comment on his answer or update your own question instead of posting it as an answer. Chances are he won't see this.
Marcus S. Zarra
Hey thanks Marcus, but there is no comment link in his answer, it just says link and flag.
Jigzat
You probably don't have enough rep to comment. In that case, edit your question instead and delete this answer. And take a look at my answer and respond please as I suspect the solution to your problem is a LOT easier.
Marcus S. Zarra
+1  A: 

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 NSManagedObjectContext?

Update

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:

  1. Create new object in persistent context
  2. get a dictionary of the attributes from the source object (use -dictionaryWithValuesForKeys and -[NSEntityDescription attributesByName] to do this.
  3. set the dictionary of values onto the target object (using -setValuesForKeysWithDictionary)
  4. 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 -[NSEntityDescription relationshipsByName]

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 :)

Marcus S. Zarra
My app has a mapView which downloads hundreds or thousands of objects to use as annotations whenever the app starts (or the user changes location). My idea was that it would be more elegant and more efficient to keep the map objects in a second MOC (scratchMOC), which would never be saved; by design, it's thrown out and reloaded whenever the app starts up. The user can then choose one or more of the map objects to add to his library, and these would then get copied/moved to the persisted MOC. I figured this would avoid iterating through a MOC to delete the thousands of map objects at app exit.
Aeonaut