Core-Data Can't pass managedObjectContext from app delegate to view controller

Posted by yahuie on Stack Overflow See other posts from Stack Overflow or by yahuie
Published on 2010-06-02T18:09:35Z Indexed on 2010/06/02 18:24 UTC
Read the original article Hit count: 306

Filed under:

I'm making a core-data application that is view based. I can create the managedObjectContext and 'use' it in the app delegate, but can not pass it to the mainviewcontroller. Probably something simple, but I can't find the problem after looking for quite a while. The managedObjectModel is nil in the mainviewcontroller.

The log and error is here:

2010-06-02 11:01:10.504 TestCoreData[404:207] Could not make MOC in MainViewController implementation.
2010-06-02 11:01:10.505 TestCoreData[404:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an     NSManagedObjectModel for entity name 'Shelf''
2010-06-02 11:01:10.506 TestCoreData[404:207] Stack: (
30864475,
2452296969,
28852395,
12038,
3217218,
10258,
2700679,
2738614,
2726708,
2709119,
2736225,
38960473,
30649216,
30645320,
2702869,
2740143,
9704,
9558

) (gdb)

Code for app delegate here:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

MainViewController *aController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
 self.mainViewController = aController;
 [aController release];
 NSLog(@"before did finish launching");

if (self.managedObjectContext == nil) {
NSLog(@"Could not make MOC in TestCoreDataAppDelegate implementation.");
}
//Just to see if I can access the here. 
 NSFetchRequest* request = [[NSFetchRequest alloc] init]; 
 NSEntityDescription* entity = [NSEntityDescription entityForName:@"Shelf"      inManagedObjectContext:self.managedObjectContext];
  [request setEntity:entity];
if (!entity) {
NSLog(@"No Entity in TestCoreDataAppDelegate didfinishlaunching");

}
 NSLog(@"passed did finish launching");
 NSManagedObjectContext *context = [self managedObjectContext];
 self.mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
 self.mainViewController.managedObjectContext = context;
 [context release];
 [window addSubview:[mainViewController view]];
[window makeKeyAndVisible];

return YES;
}

Code in MainViewController here:

@implementation MainViewController

@synthesize managedObjectContext;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
    // Custom initialization
}
return self;
}




- (void)viewDidLoad {
[super viewDidLoad];
if (self.managedObjectContext == nil) {
 NSLog(@"Could not make MOC in MainViewController implementation.");
}

NSFetchRequest* request = [[NSFetchRequest alloc] init];
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Shelf"    inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
}

Thanks.

© Stack Overflow or respective owner

Related posts about iphone