I am trying to create a temporary context for a managed object, and after several screens of the user entering the information, I combine this context with the main context (so that no "incomplete" objects are inserted). This is how I create my temporary context and how I insert an object into it:
if (!self.someManagedObject) { NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:@[[NSBundle mainBundle]]]; NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; [storeCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:nil]; NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] init]; [managedObjectContext setPersistentStoreCoordinator:storeCoordinator]; self.someManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"SomeObject" inManagedObjectContext:managedObjectContext]; NSLog(@"%@", self.someManagedObject.managedObjectContext); }
This is part of viewDidLoad . The console shows that the context of the managed entity matters.
However, right after this if statement (even inside viewDidLoad , self.someManagedObject.managedObjectContext is nil. I can understand why the local variable will no longer be available (it just goes out of scope), but the property of the managed object should still be set, right?
I know that I can create a property to hold the context of a managed entity, but I would prefer it to work that way.
iphone core-data nsmanagedobjectcontext
Scott Berrevoets
source share