Core-Data iPhone: could not locate an NSManagedObjectModel

喜你入骨 提交于 2019-11-27 03:20:46
CraigH

Ok.

Placing the following code inside of the RootViewController's viewDidLoad eliminates the error:

if (managedObjectContext == nil) 
{ 
    managedObjectContext = [(CoreDataBooksAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    NSLog(@"After managedObjectContext: %@",  managedObjectContext);
}

I found someone with a similar problem here on SO: link text

As Aryeh pointed out in that post: "In short you are trying to fetch an entity from an objectContext that hadn't been set up yet. Your options therefore are to set it up right then or do elsewhere in the app before this view loads."

In applicationDidFinishLaunching: you're doing the following:

rootViewController.managedObjectContext = self.managedObjectContext;

But I don't see the code where self.managedObjectContext is setup. applicationDidFinishLaunching is called pretty early on. Are you sure you setup the managed object context?

Did you change the Core Data model at all? That error is common when the Core Data model for an app changes without also changing the underlying SQLite database, so the stored data and the model are out of sync.

Try completely removing your app from the Simulator or testing device, then reinstalling it and trying again.

During my development, I could not find Entities that I added later on. What worked for me: (Basically a sanity-tip :-)

Uninstall the App EVERY TIME you change the Data Model!

The Data Model is cached by Core Data between installs, to make sure the integrity stays in tact. Delete the App from the simulator / iPhone to be able to test your changes.

PS: does anyone know how to do that automatically?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!