2

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

Ton
  • 365
  • 4
  • 19
  • You can pass a nil context to initWithEntity:insertIntoManagedObjectContext. See this question: http://stackoverflow.com/questions/3256195/how-to-deal-with-temporary-nsmanagedobject-instances – Symmetric Sep 26 '12 at 02:27

3 Answers3

2

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
  • 46,571
  • 9
  • 101
  • 182
  • This will fail on @dynamic properties. The doc for initWithEntity:insertIntoManagedObjectContext says "Important This method is the designated initializer for NSManagedObject. You must not initialize a managed object simply by sending it init." – Symmetric Sep 26 '12 at 02:29
  • Your comment does not make sense. My answer specific states to pass in nil for the NSManagedObjectContext. There was no implication that the bare `-init` is to be called. – Marcus S. Zarra Sep 26 '12 at 06:12
  • Ok, sorry. I read "alloc init" above as a bare init, not initWithEntity. In any case, your answer to this other question makes it more clear, thanks: http://stackoverflow.com/questions/3256195/how-to-deal-with-temporary-nsmanagedobject-instances – Symmetric Sep 26 '12 at 16:11
0

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
  • 5,007
  • 1
  • 39
  • 56
  • Nope. this isn't working. I guess it will assign the object to the default store when using nil. – Ton Jun 27 '10 at 22:11
0

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
  • 1,865
  • 16
  • 10
  • 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 Aug 07 '10 at 23:49
  • Agree with Marcus, this is how I do it, and I use a base class to handle the recursive insertion into a valid context... http://www.locassa.com/index.php/2011/05/temporary-storage-in-apples-coredata/ – Simon Lee May 22 '11 at 22:49
  • @MarcusS.Zarra, do you still not recommend to create a separate context for temporary (potentially thrown away) objects? Is it still valid in 2014? Thanks in advance. – A.S. Mar 24 '14 at 15:50
  • No, the creation of a `NSManagedObjectContext` has gotten a LOT cheaper and they are easy to create. I am still not a fan of creating multiples just to throw away but it is a lot cheaper now. – Marcus S. Zarra Mar 24 '14 at 20:15