The persistence Store coordinator will try to open the original persisted store with the new version of the template, but that is the old template, the old format, of course, error. Now all you have to do is migrate the existing persisted data area to match the new model.
How to migrate?
When does the migration take place?
When adding a store to nspersistentstorecoordinator.
So how do you add it?
The answer is that by placing the following options in Nsdictionary, the migration of the storage area will be completed automatically.
- If the nsmigratepersistentstoresautomaticallyoption passed to Nspersistentstorecoordinator is yes, then the core Data will try to migrate the low-version persistent storage to the latest version of the model
- If the nsinfermappingmodelautomaticallyoption passed to Nspersistentstorecoordinator is yes, then core Data will try to automatically infer the "Source Model entity" in the most reasonable way. Which property in the target model entity corresponds to one of the properties in the
- When you open the above two options, it is a lightweight migration (lightweight migration).
The program is modified as follows:
Note: Migrate is "migrated" and infer is "inferred".
3.3. Lightweight migration Approach (Core Data Application Practice Guide)