How to update the CloudKit schema after the app has been released to the AppStore

Hi, I recently had an issue with one of my production apps that use Core Data and CloudKit where data wasn't syncing between devices, after a little bit of research I found out that the schema in the private CloudKit container needed to be initialized; which I never did.

The part I'm still not 100% sure is when to run the initializeCloudKitSchema method after the app has been released to the AppStore. I see that Apple recommends running it when testing by using #if DEBUG, but... do you really want to run it every time you compile in Xcode?

Here is how I understand it at this point...

  1. App release, call initializeCloudKitSchema() to match schemas between Core Data and CloudKit.
  2. Added or deleted an attribute, call initializeCloudKitSchema() to update the CloudKit schema.
  3. Renamed an attribute, call initializeCloudKitSchema() to update the CloudKit schema.
  4. Etc.

If my assumption above is correct, calling the initializeCloudKitSchema() method during development would update the schema in CloudKit before the new app version is released in the AppStore, therefore creating an issue for existing users with previous versions of the app since they will not have the latest code but will be using the latest schema which contains the new attributes.

Can someone please share their method of handling schema updates in CloudKit after the app has been released to the AppStore?

Code:

do {
    try container.initializeCloudKitSchema()
} catch {
    print(error)
}

I only run it when I know I've changed something.

As far as technique, you need to be really thoughtful with your CloudKit schema since you are limited in what can change in production.

Go watch the session from this year, Evolve your Core Data schema, for some suggestions and thoughts.

CloudKit Limitations:

Mirroring a Core Data Store with CloudKit | Apple Developer Documentation

Determine If Your App Is Eligible for Core Data with CloudKit

Apps adopting Core Data can use Core Data with CloudKit as long as the persistent store is an NSSQLiteStoreType store, and the data model is compatible with CloudKit limitations. For example, CloudKit does not support unique constraints, undefined attributes, or required relationships.

How to update the CloudKit schema after the app has been released to the AppStore
 
 
Q