💭 Fix Core Data/SwiftData Cloud Sync Issues in Production

Get weekly handpicked updates on Swift and SwiftUI!

TL;DR: Cloud sync issues in Core Data/SwiftData after App Store release often stem from CloudKit’s separate dev and prod environments. Manually deploy schemas to the production environment via the CloudKit Dashboard and verify entitlement configurations for seamless syncing.

Issue

Many developers, especially first-time users of cloud sync features, encounter an issue where Core Data or SwiftData cloud sync works perfectly during development but fails to sync user data once the app is live on the App Store.

Root Cause

This issue stems from CloudKit’s dual-environment structure:

  • Development Environment: Used during the app’s development process, where schemas (data model structures) are automatically generated based on local data models.
  • Production Environment: Used for the actual app released to users.

Unless the schema is manually deployed from the development to the production environment before release, users’ data will not sync in the live app.

Solution

1. Deploy Schema to Production Environment

Manually deploy the schema from the development to the production environment via the CloudKit Dashboard. Once deployed, the production environment’s container will include the schema corresponding to the local data model.

Deploy Schema

2. Deployment Timing

Before each submission to the App Store, if there are any changes to the data model, the schema must be redeployed to the production environment.

3. Verify entitlement Configuration

Ensure the entitlement file is correctly set up for the desired environment:

  • Production (default for TestFlight): No additional changes are needed.
  • Development Environment (optional for TestFlight testing): Add the following to your entitlement file:
XML
<key>com.apple.developer.icloud-services</key>
<array>
    <string>CloudDocuments</string>
    <string>CloudKit</string>
</array>
<key>com.apple.developer.icloud-container-environment</key>
<string>Development</string>

4. Reset Development Environment (Optional)

During development, use the Reset Environment feature in the CloudKit Dashboard to clear all data and schemas in the development environment. This ensures a clean state for testing.

Reset Environment


Further Reading