Core-Data iPhone: could not locate an NSManagedObjectModel

后端 未结 4 1448
执念已碎
执念已碎 2020-11-29 23:53

I am using Apple\'s CoreDataBooks sample project as a learning aid for core data.

I modified the app so that when the app loads I show a menu page first - not the B

相关标签:
4条回答
  • 2020-11-30 00:19

    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?

    0 讨论(0)
  • 2020-11-30 00:35

    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.

    0 讨论(0)
  • 2020-11-30 00:37

    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?

    0 讨论(0)
  • 2020-11-30 00:38

    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."

    0 讨论(0)
提交回复
热议问题