Cannot create an NSPersistentStoreCoordinator with a nil model

前端 未结 27 1050
囚心锁ツ
囚心锁ツ 2021-01-30 03:33

Been having my first crack at Core Data and I\'m getting the following error when running my code on my device, but it works fine on the simulator..

*

相关标签:
27条回答
  • 2021-01-30 04:22

    If your project works on the simulator but not on the device, try running the release-build on your device instead of the debug-build.

    Select your project -> Product -> Edit Scheme -> Build Configuration [DEBUG -> RELEASE]

    Now run the project again, it will work.

    0 讨论(0)
  • 2021-01-30 04:25

    Okay, I'll tell a half-solution first, it will work if you re-install the app(in simulator or debugger device). But that's not a real solution for sure. For example, if you are updating your application, do NOT do that or your new version may crash because users won't re-install it, they'll use update button instead.

    As I understood, this problem happens mostly when you change the data model file name. The reason might be like this:
    . When you run the app for the first time, it is creating a data model file in app bundle like "data_model_1". This creation happens only for first time.
    . When you update the file name and run the app again, it won't be able to find because there is still "data_model_1" file but you are telling it to look for "data_model_2". How can it find, it has not created it yet and will not unless you don't install the app with new file name.

    So, if this is the first version of your app and re-installing on simulator or device doesn't harm your project, go for it. Else, check Core Data Model Versioning and Data Migration Guide on iOS Developer Library, maybe it's what you need.

    Edit: If reinstalling does not work, try uninstalling first, then cleaning project, then close everything, reopen project and build+run. That should work.

    0 讨论(0)
  • 2021-01-30 04:26

    If someone is stuck because of the same issue. Make sure you have properly linked the database (as you might have copied the code directly from some example).
    Just update the name of the database in managedObjectModel and persistentStoreCoordinator methods in AppDelegate.

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