Coredata collation (4) — data migration and other issues
- Lightweight data migration
- Subsequent problems
Lightweight data migration
In the process of iteration of APP version, it is inevitable to change the. Xcdatamodel file, such as adding or deleting an entity, adding or deleting the attributes of an original entity, etc. If you do not set up data migration, the original data will be emptied after the app is updated. Of course, this is not the case, so data migration is required at this time.
Core data can set lightweight data migration. The system will automatically help you analyze differences and map them. This method is only applicable to simple operations such as adding or deleting entities or attributes. In addition, there is a quite complex custom data migration, which will not be used in general. This article does not intend to explain it.
1.Set the automatic migration function in the core data stack
Add the settings related to automatic migration in the instantiation method of PSC.
If you use magicalrecord, you only need to modify the previous method of initializing coredatastack to use setupautomigratingcoredatastack for initialization.
2.Add a new version of coredata and switch to the new version
After selecting the. Scdatamodel file, click editor – > add model version
At this point, the. Xcdatatmodeld file can be expanded to see the multiple versions it contains
Follow the steps shown in the figure below to switch the current version to the version you want
Now you can make changes on the new version
- After the lightweight automatic migration is enabled, the app will judge whether there is a difference between the entity and the database table structure every time it starts. If there is a difference, the database file will be updated to a new table structure.
- If a new attribute is added to the original entity, the field in the migrated data is empty; if the attribute is reduced in the original entity, the field in the migrated data will be deleted and the corresponding data will be deleted. Even if you switch back to the original version, the data will not be recovered.
Problem 1: core data creates manageobjectclass and reports repeated definition error of model
Create MO class through editor – > create nsmanageobject sublass… And report the following error:
Since Xcode 8, the nsmanagedobject subclass of the entity in core data is automatically generated by Xcode by default, and cannot be seen in project navigator. It can be imported directly when used.
If you still want to use the development method of “create nsmanagedobject subclass” to generate files, you can find the “CodeGen” option of entity (located in the data model inspector), change the default “class definition” of “CodeGen” to “manual / none”, and then use “create nsmanagedobject subclass” to create them.
Coredata collation (I) — basic concepts and simple use
Coredata collation (2) – multithreading scheme
Coredata arrangement (3) — the use of magic record
Coredata data migration
Define core data migration