简体   繁体   中英

Core Data - lightweight migrations and multiple core data model

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.

导航器scrrenshot

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

  • user1 updates every time a new version of the app is released, however in the latest update, this correlates with the third oldest data model version.
  • user2 has not updated the app for four months - three App Store release / versions ago, which happens to correlate with the seventh oldest data model version.
  • user3 was using an Android phone, realised the error of his ways, and returned to his iPhone 4, with your app installed, but not updated for one year, which correlates with the nineteenth oldest data model version, when the app used two different data model containers.

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:

  • user1's version of the app has three data model versions to migrate to arrive at an SQLite database that aligns with the latest data model.
  • user2's version of the app has seven data model versions to migrate to arrive at an SQLite database that aligns with the latest data model.
  • user3's version of the app has nineteen data model versions to migrate, but to add to this, the two previous data models must merge between data models version 10 and data models version 11 (for example), to arrive at an SQLite database that aligns with the latest data model.

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

  • Keep ALL previous data models and versions in their respective .xcdatamodeld containers.
  • In the case that you have more than one data model that must migrate, keep the ORIGINAL versions of these data model .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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM