CoreData error: +entityForName: could not locate an NSManagedObjectModel for entity name

后端 未结 6 595
失恋的感觉
失恋的感觉 2021-01-02 09:06

I\'ve been struggling with CoreData for a few days, but I keep getting this error:

\'NSInternalInconsistencyException\', reason: \'+enti

相关标签:
6条回答
  • 2021-01-02 09:13

    This is a fairly common error and it has three causes:

    1. Misspelling the entity name e.g. NewsStand instead of NewsStands.
    2. Having a nil managed object context
    3. Having no or the wrong managed object model loaded.

    (1) is the most common but (3) is more common than (2). You can check that you are loading the right model with the keypath:

    aManagedObjectContext.persistentStoreCoordinator.managedObjectModel.entities
    

    then check the entity's names.

    0 讨论(0)
  • 2021-01-02 09:20

    Don't have the rep to comment - but Luc Bloom's response fixed MY issue. I completely forgot I changed some stuff in the data model after initial build/install and spent entirely too long banging my head against the desk.

    0 讨论(0)
  • 2021-01-02 09:20

    If you're editing a framework and running a unit test to get the error, make sure your xcdatamodeld file is added to the test target. Frameworks behave differently than normal projects.

    0 讨论(0)
  • 2021-01-02 09:25

    Ensure that coredata.managedObjectContext is not nil.

    0 讨论(0)
  • 2021-01-02 09:29

    I had a similar problem and found TechZen's answer to be helpful (especially the suggestion to check the entities). However, my problem turned out to be a variant of (2): I could see that the moc itself wasn't nil, but I hadn't set the persistent store coordinator.

    [aManagedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator]
    

    or similar.

    I'd add this as a comment to TechZen's answer, but can't yet, and want to include it in case someone else has my problem.

    0 讨论(0)
  • 2021-01-02 09:32

    During my development, I could not find Entities that I added later on. What worked for me:

    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)
提交回复
热议问题