2
votes

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

3
You can pass a nil context to initWithEntity:insertIntoManagedObjectContext. See this question: stackoverflow.com/questions/3256195/…Symmetric

3 Answers

2
votes

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.

0
votes

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

0
votes

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.