I need to create NSManagedObject
instances, do some stuff with them and then trash them or store to sqlite db. The problem is, I cannot create instances of NSManagedObject
unconnected to NSManagedObjectContext
and this means I have to clear up somehow after I decide that I don't need some of the objects in my db.
To deal with it, I have created an in-memory store using the same coordinator and I'm placing temporary objects there by using assignObject:toPersistentStore.
Now, how do I ensure that these temporary objects don't get to the data, which I fetch from the common to both stores context? Or do I have to create separate contexts for such a task?
UPD:
Now I'm thinking about making separate context for in-memory store. How do I move objects from one context to another? Just using [context insertObject:]? Will it work OK in this setup? If I insert one object from the graph of objects, does the whole graph also get inserted into context?
The correct way to achieve this sort of thing is with a new managed object context. You create a managed object context with the same persistent store:
Then you add new objects, mutate them, etc.
When it comes time to save, you need to call [tempContext save:...] on the tempContext, and handle the save notification to merge that into your original context. To discard the objects, just release this temporary context and forget about it.
So when you save the temporary context, the changes are persisted to the store, and you just need to get those changes back into your main context:
This is also the way you should handle multi-threaded core data operations. One context per thread.
If you need to access existing objects from this temporary context (to add relations etc.) then you need to use the object's ID to get a new instance like this:
If you try to use an
NSManagedObject
in the wrong context you will get exceptions while saving.What you are describing is exactly what an
NSManagedObjectContext
is for.From Core Data Programming Guide: Core Data Basics
And Core Data Programming Guide: Managed Object Validation
NSManagedObjectContext
s are designed to be lightweight. You can create and discard them at will - it's the persistent stores coordinator and it's dependancies that are "heavy". A single persistent store coordinator can have many contexts associated with it. Under the older, obsolete thread confinement model this would mean setting the same persistent store coordinator on each context. Today it would mean connecting nested contexts to a root context that is associated with the persistent store coordinator.Create a context, create and modify managed objects within that context. If you want to persist them and communicate those changes, save the context. Otherwise discard it.
Attempting to create managed objects independent of an
NSManagedObjectContext
is asking for trouble. Remember that Core Data is ultimately a change tracking mechanism for an object graph. Because of this, managed objects are really part of the managed object context. The context observes their life cycle, and without the context not all of the managed object functionality will work correctly.