views:

56

answers:

3

Hi,

I am using a tableview with data from coredata using nsfetchedresultscontroller. When the view loads i make a new entity using

SomeManagedObject *someManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"SomeManagedObject" inManagedObjectContext:self.managedObjectContext];

This way the new entity appears in my tableview. Now i want this entity to be only temporary, but when i edit some object inside the tableview and save the managedObjectContext the temporary entity will also get saved and i don't want that.

Is their a way to save one object only and not everything inside de managedObjectContext?

Is their some other way to make a temporary object for my tableview.

Any help would be very welcome. Thanks Ton

A: 

No, in a managedObjectContext saving is a all or nothing. What I do not know is what happens if you set the persistent store of the managed object to nil

- (void)assignObject:(id)object toPersistentStore:(NSPersistentStore *)store

If you then save the managedObjectContext this object should not be saved. It is just a guess, but tell me if it works ;-)

GorillaPatch
Nope. this isn't working. I guess it will assign the object to the default store when using nil.
Ton
A: 

For temporary managed objects, create them with a 2nd managed object context (MOC). When you are finished, simply release the MOC without performing a save.

Look at the Adding a Book code in CoreDataBooks which uses the same approach to throw away the newly added object when the user cancels.

Scott McCammon
Do not create a separate context. That is extremely wasteful and expensive. If the object must be temporary, create it with -init and do not pass in a context at all.
Marcus S. Zarra
+1  A: 

Create the new NSManagedObject with it's alloc init and pass nil instead of the NSManagedObjectContext. Then if you later decide you want that object to be permanent then set it's context. However this will not allow you to see it in a NSFetchedResultsController because it will not be associated with the context.

A better answer can be provided if you could explain what your ultimate goal is.

Marcus S. Zarra