Hi, I'm working on a macOS app that utilizes SwiftData to save some user generated content to their private databases.
It is not clear to me at which point the app I made starts using the production database. I assumed that if I produce a Release build that it will be using the prod db, but that doesn't seem to be the case.
I made the mistake of distributing my app to users before "going to prod" with CloudKit. So after I realized what I had done, I inspected my CloudKit dashboard and records and I found the following:
- For my personal developer account the data is saved in the Developer database correctly and I can inspect it.
- When I use the "Act as iCloud account" feature and use one of my other accounts to inspect the data, I notice that for the other user, the data is neither in the Development environment nor the Production environment. Which leads me to believe it is only stored locally on that user's machine, since the app does in fact work, it's just not syncing with other devices of the same user.
So, my question is: how do I "deploy to production"?
- I know that there is a Deploy Schema Changes button in the CloudKit dashboard. At which point should I press that? If I press it now, before distributing a new version of my app, will that somehow "signal" the already running apps on user's machines to start using the Production database?
- Is there a setting in Xcode that I need to check for my Release build, so that the app does in fact start using the production db?
- Is there a way to detect in the code whether the app is using the Production database or not? It would be useful so I can write appropriate migration logic, since I don't want to loose existing data users already have saved locally.
For my personal developer account the data is saved in the Developer database correctly and I can inspect it.
If it was a development version built and ran with Xcode, the data would go to the development environment, and so this is as-designed; if it is a production version downloaded from the App Store, or a TestFlight version, no, the data should not appear in the development environment.
When I use the "Act as iCloud account" feature and use one of my other accounts to inspect the data, I notice that for the other user, the data is neither in the Development environment nor the Production environment. Which leads me to believe it is only stored locally on that user's machine, since the app does in fact work, it's just not syncing with other devices of the same user.
If you haven't deployed the CloudKit schema to the production environment, the CloudKit synchronization of your App Store app will hit an error, and the data will stay on the device and will not be synchronized across devices.
So, my question is: how do I "deploy to production"?
This is covered in Deploying an iCloud Container’s Schema.
I know that there is a Deploy Schema Changes button in the CloudKit dashboard. At which point should I press that?
You deploy your CloudKit container to the production environment after you have finalized your CloudKit schema and fully tested your app in the development environment.
In a production environment, you can only make additive changes.
If I press it now, before distributing a new version of my app, will that somehow "signal" the already running apps on user's machines to start using the Production database?
The apps on user's machines should always use the production environment. After you deploy your container to the production environment, the CloudKit synchronization of the apps should start to work.
Is there a setting in Xcode that I need to check for my Release build, so that the app does in fact start using the production db?
CloudKit uses the com.apple.developer.icloud-container-environment
entitlement to determine which environment it uses.
By default, Xcode takes care the entitlement for you: It automatically picks the development environment in the development phase, and picks the production environment when you submit your app to App Store or TestFlight.
People may configure the entitlement manually for some reasons (for testing the production environment in the development phase, for example). I don't think you need to do so in your case.
Is there a way to detect in the code whether the app is using the Production database or not?
There isn't.
In your case, the data should start to synchronize after you successfully deploy your container to the production environment, and so you probably don't need to worry.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.