Changing the CoreData data model for an iOS application with iCloud synchronization

The seemingly simple task of adding attribute(s) to the CoreData data model can turn into problems if the application uses synchronization between devices via iCloud. In this case, it is necessary to carry out a so-called lightweight migration of the previous database to a database with a new model, otherwise synchronization will stop working, which can lead to further data loss when restoring it from iCloud. Here are step-by-step instructions on how to do this easily and safely using my example. financial accounting applicationswhich is used daily by several thousand users, so data security must be taken seriously

So, let's go!

1. Add a new version of the model

To do this, in Xcode, select your model (in this case it is Debts.xcdatamodeld) and in the top Edit menu select Add Model Version…

I named the new version of the model Debts_v2

I named the new version of the model Debts_v2

Select the newly created new model file in Xcode on the left

and add the attributes we need

in the inspector menu on the right we indicate that starting from this version of the application we will use the new version of the Debts_v2 data model

After this, a green check mark should appear on the new version of the model in Xcode on the left

2. Initializing a new schema in CloudKit Database

For everything to work correctly, you need to initialize a new schema in CloudKit Database. corresponding to your new model. To do this, you need to add these two lines of code to the initializer of your NSPersistentCloudKitContainer

Now run the application on a real device under your developer AppleID (it may work on the simulator, but I haven’t tried it), wait until the application starts and the console in Xcode completely calms down, then delete the application from the device and comment out these two lines in the code.

3. Deploying a new schema in Production

Our new scheme now exists only in Development; in order for it to work in Production, it needs to be deployed there. To do this you need to login to your CloudKit Database

and click Deploy Schema Changes at the bottom left.

Something like this should appear. If zeros come out, it means you did something wrong above.

Click Deploy and you're done!

Click Deploy and you're done!

All. Now you can use the new attributes in your code and submit your application to the AppStore.

After the new version appeared in the AppStore, I checked the synchronization as follows.

  • applications on iPhone and iPad previously downloaded from the AppStore are both not updated – synchronization works as before, without problems

  • I updated the applications on the iPhone, but not on the iPad – synchronization works in both directions, as before, without problems

  • The applications on the iPhone and iPad are both updated – at the beginning there was a minute delay in synchronization, after a minute and then it works as before almost instantly, without problems.

    Here you can download my application and check how synchronization and data recovery works after deleting the application or restoring data from iCloud on a new device.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *