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?
Depending on your use of the temporary object there are some caveats to the above recommendations. My use case is that I want to create a temporary object and bind it to views. When the user opts to save this object, I want to setup relationships to existing object(s) and save. I want to do this to avoid creating a temporary object to hold those values. (Yes, I could just wait until the user saves and then grab the view contents but I'm putting these views inside of a table and the logic to do this is less elegant.)
The options for temporary objects are:
1) (Preferred) Create the temporary object in a child context. This won't work because I'm binding the object to the UI and I can't guarantee the object accessors are called on the child context. (I have found no documentation that states otherwise so I have to assume.)
2) Create the temporary object with nil object context. This doesn't work and results in data loss/corruption.
My Solution: I solved this by creating the temporary object with nil object context but when I save the object, rather than inserting it as #2, I copy all of it's attributes into a new object that I create in the main context. I created a supporting method in my NSManagedObject subclass called cloneInto: that lets me copy attributes and relationships easily for any object.
For me Marcus's answer didn't work. Here's what worked for me:
then, if I decide to save it:
We must also not forget to release it
iOS5 provides a simpler alternative to Mike Weller's answer. Instead use a child NSManagedObjectContext. It removes the need to trampoline through NSNotificationCenter
To create a child context:
Then create your objects using the child context:
The changes are only applied when the child context is saved. So to discard the changes just do not save.
There is still a limitation on relationships. ie You can't create relationships to objects in other contexts. To get around this use objectID's, to get the object from the child context. eg.
Note, saving the child context applies the changes to the parent context. Saving the parent context persists the changes.
See wwdc 2012 session 214 for a full explanation.
I am rewriting this answer for Swift as all similar questions for swift redirect to this question.
You can declare the object without any ManagedContext using the following code.
Later on, to save the object you can insert it into the context and save it.
Creating temporary objects from nil context works fine until you actually try to have a relationship with an object whose context != nil!
make sure your okay with that.
NOTE: This answer is very old. See comments for full history. My recommendation has since changed and I no longer recommend using unassociated
NSManagedObject
instances. My current recommendation is to use temporary childNSManagedObjectContext
instances.Original Answer
The easiest way to do this is to create your
NSManagedObject
instances without an associatedNSManagedObjectContext
.Then when you want to save it: