Sync States
Understanding CloudKit synchronization lifecycle and monitoring sync progress.
Sync States
CloudKit sync operates in phases:
1. Not Syncing
Indicator: No pending changes, no active network operations.
Conditions:
context.hasChanges == falseNo CloudKit export/import tasks running
2. Pending Upload
Indicator: Local changes waiting for CloudKit export.
Trigger: After context.save() with modified objects.
Duration: Immediate to 30 seconds (depends on network, CloudKit queue).
3. Uploading
Indicator: CloudKit exporting records to Private/Shared Database.
Monitoring:
4. Pending Download
Indicator: Remote changes detected, queued for import.
Trigger: Push notification from CloudKit with new data.
5. Downloading
Indicator: CloudKit importing records to local store.
Event:
6. Synced
Indicator: Local and remote data consistent.
User Feedback: Show checkmark, "All changes saved".
Event Notifications
Subscribe:
Event Types:
.setup: Initial CloudKit setup.import: Downloading from CloudKit.export: Uploading to CloudKit
Event Properties:
UI Sync Indicators
Simple Indicator:
Advanced: Per-Entity Sync: Track which entities are syncing:
Background Sync
Automatic: iOS performs background sync when app is inactive.
Configuration (already enabled in TheCompanyApp):
Limitations:
Requires Background Modes capability
iOS controls timing (battery, network)
Not guaranteed immediate
Manual Sync Trigger
User-Initiated Refresh:
Pull-to-Refresh Example:
Offline Behavior
Local-First: All operations work offline.
Queue: Changes saved to local store, sync when network available.
Indicator:
Sync Errors
Common Errors:
CKErrorNetworkUnavailable: No internetCKErrorNotAuthenticated: Not signed in to iCloudCKErrorQuotaExceeded: iCloud storage fullCKErrorZoneBusy: Too many requests, retry later
Handling:
Related: Sync Architecture, Remote Change Handling, Troubleshooting
Last updated