New to OS X programming. Started with an Xcode template for a document-based app with Core Data.
In my default Document.xib I've created a View
which I'm controlling with a custom ViewController
. I then created a Managed Object Context
in Document.xib and created two outlets, one to Document.h
:
@property (strong) IBOutlet NSManagedObjectContext *myManagedObjectContext;
and one to ViewController.h
:
@property (weak) IBOutlet NSManagedObjectContext *myManagedObjectContext;
In windowControllerDidLoad
in Document.m
, I then added self.myManagedObjectContext = [self managedObjectContext]
.
Following ghostfly's advice (could not locate an NSManagedObjectModel for entity name) I added:
NSLog(@"Context: %@",self.myManagedObjectContext);
NSLog(@"PS Coord : %@",self.myManagedObjectContext.persistentStoreCoordinator);
NSLog(@"MOM : %@", self.myManagedObjectContext.persistentStoreCoordinator.managedObjectModel);
NSLog(@"Entities : %@",[[self.myManagedObjectContext.persistentStoreCoordinator.managedObjectModel entities] valueForKey:@"name"]);
to my Document.m in windowControllerDidLoad
and also in my ViewController's awakeFromNib
. In Document.m everything seems to work fine: all the NSLog statements look right, and I can also add Entities into my NSManagedObjectContext but in the ViewController, only the first NSLog statement works, and the rest return (null).
My question: What's going wrong here, and am I even going about this the right way?
Various other questions seem to suggest either adding an AppDelegate to MainMenu.xib
, but I'm not sure how this would work in practice in a Document-based application (for example, I'd expect each Document to have a separate NSManagedObjectContext
, but if I use an AppDelegate, then surely they're all the same?), or even if that's recommended because some tutorials suggest this is explicitly not how to do it (e.g. here http://franck.verrot.fr/blog/2012/01/18/best-way-to-pass-nsmanagedobjectcontext-around-in-ios-applications/). Help is much appreciated! Thanks.