views:

35

answers:

1

I have an NSViewController with its own nib that requires access to the managedObjectContext to display objects in a tableview. This is how I have the managed object context set up in my view controller:

@interface {
    ...
    NSManagedObjectContext* managedObjectContext;
    ...
}

@property (nonatomic, retain) NSManagedObjectContext* managedObjectContext;

@implementation {
    ...
    @synthesize managedObjectContext;
    ...
}

in awakeFromNib I initialize it as follows:

managedObjectContext = [[NSApp delegate] managedObjectContext];

I have the tableview hooked up to an NSArrayController in interface builder (exactly as in this tutorial) and I have its managedObjectContext set to my view controller's managedObjectContext.

What could be the source of this error?

A: 

Set a breakpoint in your -awakeFromNib method and verify your app delegate returns a valid object or is a valid object itself at this point. I'll bet it's nil. The context may not be created yet because your view controller may have been awoken before the app delegate.

Joshua Nozzi
Thanks for the tip, I was able to follow the stack trace and found out that my awakeFromNib was being called twice (silly Interface Builder hookup error), causing that behavior, as well as a bunch of other problems.
David K