Coredata collation (4) — data migration and other issues

Time:2019-12-9

Coredata collation (4) — data migration and other issues


Catalog

  • 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.
Coredata collation (4) -- data migration and other issues

If you use magicalrecord, you only need to modify the previous method of initializing coredatastack to use setupautomigratingcoredatastack for initialization.

Coredata collation (4) -- data migration and other issues

2.Add a new version of coredata and switch to the new version

After selecting the. Scdatamodel file, click editor – > add model version
Coredata collation (4) -- data migration and other issues

At this point, the. Xcdatatmodeld file can be expanded to see the multiple versions it contains
Coredata collation (4) -- data migration and other issues

Follow the steps shown in the figure below to switch the current version to the version you want
Coredata collation (4) -- data migration and other issues

Now you can make changes on the new version
Coredata collation (4) -- data migration and other issues

ps:

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

Subsequent problems

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:
Coredata collation (4) -- data migration and other issues

Solve:
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 (4) -- data migration and other issues

Reference: Zhihu


review

Coredata collation (I) — basic concepts and simple use
Coredata collation (2) – multithreading scheme
Coredata arrangement (3) — the use of magic record
Demo address


Reference articles

Coredata data migration
Define core data migration

Recommended Today

Docker learning (5) basic command of dockerfile

To learn dockerfile, you need to understand its basic commands From – base image Try to use the official reference image [x] From Tomcat: 8.5.50-jdk8-openjdk 񖓿 make reference image (based on Tomcat: 8.5.50-jdk8-openjdk) [x] From CentOS ා make a base image based on CentOS: latest [x] From scratch? Does not depend on any reference image […]