Re: Managed Object Context Reference
Re: Managed Object Context Reference
- Subject: Re: Managed Object Context Reference
- From: Quincey Morris <email@hidden>
- Date: Mon, 31 May 2010 22:53:03 -0700
On May 31, 2010, at 22:26, Richard Somers wrote:
> 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];
...
> 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?
Yes. 'currentDocument' can be nil. :)
The NSDocumentController class reference documentation explains one scenario (drag and drop) where your application may reference it while it's nil.
You've created another scenario by making your view need the managed object context (probably during 'drawRect:' at least?). If the view has to redraw when your application is in the background, you'll hit your assert.
Notice also this uncovers a more serious bug in your current design. If the view has to redraw when another of your document windows is frontmost, it's going to draw with the wrong managed object context.
Going via the window controller seems like the best bet. Just pay attention to the times when objects in the referencing key path chain might be nil.
_______________________________________________
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