My submitted app has two coredata models and I made some changes to the model by adding some attributes to the current model. so I added a new version to the model and I enabled lightweight migration but this error appear when migrating reason = "Can't find model for source store"
I follow the second answer at this question [ Core Data - lightweight migrations and multiple core data model files (xcdatamodel) ][2] and it works great at simulator but not work at devices and fire the same error.
So maybe a step-by-step explanation is best...?
I encountered confusion when learning about data models, so I am presenting my own ideas about the issues that I stumbled over during this part of my Core Data education... (which is, by the way, only really just begun in the overall scheme of things).
I cannot stress enough the importance of reading a couple of good books and developing a solution based on the advice contained within... so with that in mind...
A book that I often recommend for those interested in Core Data is from The Pragmatic Bookshelf – "Core Data, 2nd Edition, Data Storage and Management for iOS, OS X, and iCloud" (Jan 2013) by Marcus S. Zarra, and in particular Chapter 3 titled "Versioning and Migration”.
It is important to recognise that to migrate successfully, Core Data requires ALL PREVIOUS ORIGINAL INTACT UNALTERED VERSIONS of the data model.
Why?
An example...
So how is Core Data to know how to migrate the previous app's SQLite database to the current version, so that the database will work with the code in your app?
(Now I don't understand this entirely so please forgive my ignorance, but) my understanding is that Core Data uses the hash signed values of previous versions of your data model to identify which data model your app is currently using, and based on that, applies migration to update your data model - and here is the important part - ONE DATA MODEL VERSION AT A TIME !
This is critical to understand. When you understand this, you understand that Core Data requires ALL previous data model versions, unmodified, to migrate successfully. Each previous data model version is required to successfully complete each step in the migration process.
Let's review my example.
When they download the app update, and run the app for the first time following this latest update:
So if you remove or alter any of the previous data model containers or versions, how is Core Data to know how to successfully migrate?
With this in mind, I provide the following advice...
.xcdatamodeld
containers. .xcdatamodeld
containers, and use the appropriate Core Data methods to merge the containers when necessary. In direct response to the question, I suspect that at some stage you have modified the previous data model containers or versions, to suit your testing on the simulator. When testing on a device, the "different" hash signed values for the data models on device do not match anything that remains in your data model version containers, and so that Build & Run throws the error you noted in your question.
My advice is to rebuild your data model version containers ( .xcdatamodeld
files) as they were, to enable Core Data to properly migrate through all previous versions (and merge model containers as necessary) to arrive at the appropriate, and latest, SQLite database.
Hope this helps.
Let me know if I have missed the mark, and I will continue my investigation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.