Re: Managed Object Context Reference
Re: Managed Object Context Reference
- Subject: Re: Managed Object Context Reference
- From: Richard Somers <email@hidden>
- Date: Mon, 31 May 2010 23:26:38 -0600
On May 31, 2010, at 3:54 PM, Quincey Morris wrote:
You can set it yourself.
If you give the view an outlet to File's Owner, you can do it in the
view's awakeFromNib:
self.managedObjectContext =
self.windowControllerOutlet.document.managedObjectContext;
// or self.managedObjectContext =
self.documentOutlet.managedObjectContext; (if File's Owner isn't the
window controller)
If you don't want to have the extra outlet, you can do it in the
view's didMoveToWindow:
self.managedObjectContext =
self.window.windowController.document.managedObjectContext;
// or self.managedObjectContext =
self.window.delegate.managedObjectContext; (if there isn't a window
controller at all)
Of course, if you can do the above, you don't need the
managedObjectContext outlet at all -- the view can find the managed
object context at any time (after awakeFromNib or didMoveToWindow)
using a key path reference like the ones above.
Thanks for the tip. I don't think I ever would have thought of giving
the custom view an outlet to the File's Owner.
Currently I have been getting a reference to the managed object
context on the fly as needed using the document controller. This has
worked fine, except today I got a "Document is nil." assertion
failure. I have not been able to repeat the failure and have no idea
what caused it. I have been using the following code to get the moc.
NSDocumentController *controller = [NSDocumentController
sharedDocumentController];
NSPersistentDocument *document = [controller currentDocument];
NSAssert(document, @"Document is nil.");
NSManagedObjectContext *moc = [document managedObjectContext];
On May 31, 2010, at 4:04 PM, Quincey Morris wrote:
BTW, if it can happen that the window is loaded before the window
controller is added to the the document's window controller list,
windowControllerOutlet.document would still be nil at awakeFromNib
time. Handling that case requires a bit of extra work.
I have been using the standard NSDocumentController and
NSWindowController. So I don't think this is why my document was nil.
But I have a question. Is getting the managed object context from the
window controller more reliable than getting it from the shared
document controller?
--Richard
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden