Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Core Data: Initializing Managed Object without a context

Is there a way to initialize a managed object outside of a context. I'm basically trying to alloc/init a Managed Object outside of a context first, then figure out if I really want to insert the object, and then inject it into the datastore using an existing managed object context.

Is this possible, or does it go against the intended usage of Core Data?

like image 656
WillF Avatar asked Sep 28 '09 16:09

WillF


2 Answers

Managed Object are "managed" by the context, therefore you cant really instanciate them with alloc since they are not meant to be.However, instantiating a managed object through the context does not persist it until you call save method on the context, so you would have the same effect using the context to instanciate it and only saving after you figure out that you really want to use the object.

like image 78
Daniel Avatar answered Oct 13 '22 10:10

Daniel


No, you cannot instantiate an NSManagedObject instsance outside of an NSManagedObjectContext (well, you can, but bad things will happen and your program will almost certainly not work as you'd hoped). You can, however, create an NSInMemoryPersistentStore-backed NSManagedObjectContext. It's slightly more setup (not much) and everything vanishes when you dealloc the in-memory store. In the mean time, you get all the benefits of Core Data's object graph management.

like image 22
Barry Wark Avatar answered Oct 13 '22 10:10

Barry Wark