1
votes

I have a strange error in my app, which says:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Book'

I know, there are hundrets of "Could not locate an NSManagedObjectModel for entity name" topis here and on the web, but the strange thing is, it's a universal app and the iphone app always works fine, only the ipad app is crashing on startup.

In the main AppDelegate, there is some code in two methodes and in the iphone / ipad AppDelegate I'm calling this code in applicationdidFinishLaunchingWithOptions like this:

    if ([self modelExists] == NO) {
    [self buildModel];
}

So it's the same way I call the code, but the ipad version crashes and the iphone version does not.

The only different is that the iPhone version uses a TabBarContoller (set up in IB) and the iPad version uses a single viewController (also set up in IB).

It happens on both, simulator and device.

I have no idea what to do. Hope you can understand what I mean ... Thx a lot Sebastian

EDIT: I found out, when I run the iPhone Version, the code in the main AppDelegate is called as it should be, but when I run the iPad Version NONE code of the main appDelegate is called at all, so there is no managedObject created and that's the reason for the error. But why is no code run in the main AppDelegate ? Thx

EDIT2: This is the code in my main AppDelegate now:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([self modelExists] == NO) { // Checks if the model is allready filled up or not. (Uses  CoreData stuff of course)
// iPhone Version is fine here. iPad Version crashes.
    [self buildModel];
}    

[self buildInterface]; // Called in the iPhone or iPad AppDelegate to make the window visible etc.
return YES;
}

So didFinishLaunchingWithOptions is called in the iphone and in the ipad version. The iPad version just doesn't run the coredata stuff anyway, whereas the iphone version does run the coredata stuff as it should. Any idea what could be wrong? THX!

4
Any possibility the iPad has a dirty or old version of data stored in core data? I.e. Have you tried to delete the app from the iPad and reinstall to verify that core data is clean?Rob
Thx, but I deleted the app several times and reset the simulator and of course made a clean buil ...wolfrevo

4 Answers

1
votes

Maybe the app delegate is not running any code if it's just not set as an delegate of the application.

Look in your main NIB for the iPad version and make sure the "AppName App Delegate" is set as the delegate of the File's owner of that NIB.

1
votes

I found my problem. Really strange ...

It was the code of "modelExists"

- (BOOL)modelExists {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:__managedObjectContext]; //<- Crashed. Had to change it to self.managedObjectContext
request.predicate = nil;
NSError *error = nil;
...

Sebastian

0
votes

I had this same problem. My app had been working fine for weeks in development, then suddenly it was crashing with this error. Changing managedObjContect to [self managedObjectContext] solved the problem.

I would love to know why....any experts out there? Why would the original code be able to resolve the call to managedObjectContext to the member function's implementation....and suddenly not be able to? There is no other static implementation visible to this code as far as I know.

Thank for posting this, save me many hours of messing around.

0
votes

In my project, I had a navigation controller and I was getting this error when I tried to segue into a child view control.

The problem was that I needed to pass set the managedObjectContext. This is taken from the CoreDate Master/Detail example.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        [[segue destinationViewController] setDetailItem:object];
        // set the managedObjectContext, too, if you need it
        [[segue destinationViewController] setManagedObjectContext:self.managedObjectContext];
    }
}

Also, double check the segue identifier in Interface Builder matches what you have in this function (showDetail in this example).