cloudkit-sync
Skill777 starsChanged 5 months agoMITView on GitHub
Generate CloudKit sync infrastructure using CKSyncEngine with conflict resolution, sharing, and account monitoring. Use when adding iCloud sync to an iOS/macOS app.
In this repository183 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- app-intentsSkill
- foundation-modelsSkill
- apple-intelligenceSkill
- visual-intelligenceSkill
- ad-attributionSkill
- app-description-writerSkill
- apple-search-adsSkill
- iap-finalizerSkill
- keyword-optimizerSkill
- marketing-strategySkill
- originality-checkSkill
- ratings-mechanicsSkill
- rejection-handlerSkill
- review-response-writerSkill
- screenshot-plannerSkill
- app-storeSkill
- web-presenceSkill
- core-mlSkill
- animation-patternsSkill
- game-feelSkill
- liquid-glassSkill
- sf-symbolsSkill
- designSkill
- typographySkill
- ui-prototypingSkill
- ux-writingSkill
- attributed-stringSkill
- foundationSkill
- accessibility-generatorSkill
- account-deletionSkill
- analytics-setupSkill
- announcement-bannerSkill
- app-clipSkill
- app-extensionsSkill
- app-icon-generatorSkill
- app-store-assetsSkill
- auth-flowSkill
- background-processingSkill
- ci-cd-setupSkill
- consent-flowSkill
- custom-product-pagesSkill
- data-exportSkill
- debug-menuSkill
- deep-linkingSkill
- error-monitoringSkill
- feature-flagsSkill
- featuring-nominationSkill
- feedback-formSkill
- force-updateSkill
- http-cacheSkill
- image-loadingSkill
- in-app-eventsSkill
- lapsed-userSkill
- live-activity-generatorSkill
- localization-setupSkill
- logging-setupSkill
- milestone-celebrationSkill
- networking-layerSkill
- offer-codes-setupSkill
- The first 60 shown.
The file
Show its rules and text
We split this skill into the rules it gives an agent
16 rules. About 0 are common, 11 are distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈11
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 16
Use this skill when the user:
- Distinctive: line 27
Before generating, ALWAYS check:
- Distinctive: line 54
If deployment target is below iOS 17 / macOS 14, warn the user that CKSyncEngine is not available and suggest either raising the target or …
- Distinctive: line 58
If existing CloudKit code is found:
- Distinctive: line 59
Ask: Replace existing implementation, extend it, or migrate to CKSyncEngine?
- Distinctive: line 63
Ask user via AskUserQuestion:
- Distinctive: line 89
Read code templates from this skill:
- Very specific: line 117
If Sources/ exists -> Sources/CloudSync/
- Distinctive: line 142
Check "Remote notifications" (for push-based sync triggers)
- Very specific: line 304
Add CKRecord field mapping in RecordMapping.swift
- Very specific: line 306
Update nextRecordZoneChangeBatch() to include pending changes for the new type
- Very specific: line 319
Add ShareManager to handle CKShare lifecycle
- Distinctive: line 326
Verify device is signed into iCloud (Settings > Apple Account)
- Distinctive: line 327
Check entitlements match the CloudKit container identifier exactly
- Distinctive: line 329
Look for CKError logs in Console.app -- filter by "CloudKit"
- Very specific: line 330
Ensure CKSyncEngine is started (not just initialized)
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
CloudKit Sync Generator
Generate production-ready CloudKit sync infrastructure using CKSyncEngine (iOS 17+ / macOS 14+), the modern replacement for manual CKOperation chains.
When This Skill Activates
Use this skill when the user:
- Asks to "add iCloud sync" or "sync data across devices"
- Mentions "CloudKit", "CKSyncEngine", or "cloud sync"
- Wants to "share data between users" via iCloud
- Asks about "conflict resolution" for synced data
- Mentions "CKRecord", "CKShare", or "CKRecordZone"
Pre-Generation Checks
1. Project Context Detection
Before generating, ALWAYS check:
# Check deployment target (CKSyncEngine requires iOS 17+ / macOS 14+)
grep -r "platform" Package.swift 2>/dev/null || true
grep -r "IPHONEOS_DEPLOYMENT_TARGET\|MACOSX_DEPLOYMENT_TARGET" --include="*.pbxproj" | head -3
# Find existing CloudKit implementations
rg -l "CKSyncEngine\|CKContainer\|CKRecord\|CKOperation" --type swift | head -10
# Check for existing entitlements
find . -name "*.entitlements" -exec cat {} \; 2>/dev/null | grep -i "icloud"
# Check existing persistence layer
rg -l "@Model\|NSManagedObject\|PersistentModel" --type swift | head -5
# Check for existing sync infrastructure
rg "CKSyncEngineDelegate\|CKSubscription\|CKFetchRecordZoneChanges" --type swift | head -52. Compatibility Verification
CKSyncEngine requires:
- iOS 17.0+ / macOS 14.0+ / watchOS 10.0+ / tvOS 17.0+
- CloudKit entitlement
- Active iCloud account on device
If deployment target is below iOS 17 / macOS 14, warn the user that CKSyncEngine is not available and suggest either raising the target or using the older CKOperation approach (which this generator does not cover).
3. Conflict Detection
If existing CloudKit code is found:
- Ask: Replace existing implementation, extend it, or migrate to CKSyncEngine?
Configuration Questions
Ask user via AskUserQuestion:
- What data needs syncing?
- Provide your model types (e.g., Note, Task, Document)
- What properties does each model have?
- Database scope?
- Private only (user's own data across their devices)
- Private + Shared (enable CKShare for collaboration)
- Conflict resolution strategy?
- Server-wins (simplest -- always accept server version)
- Client-wins (always push local version)
- Timestamp-based merge (most recent modification wins)
- Custom merge (field-level merge logic)
- Existing persistence layer?
- SwiftData (will generate CKRecord <-> SwiftData bridging)
- Core Data (will generate CKRecord <-> NSManagedObject bridging)
- Custom / in-memory (will generate standalone CKRecord mapping)
- None yet (will generate lightweight local store + sync)
Generation Process
Step 1: Read Templates
Read code templates from this skill:
templates.md- All CKSyncEngine code templates
Step 2: Create Core Files
Generate these files based on configuration:
Always generate:
Sources/CloudSync/
├── SyncEngine.swift # CKSyncEngine setup + CKSyncEngineDelegate
├── SyncConfiguration.swift # Zone names, container ID, database scope
├── RecordMapping.swift # CKRecord <-> local model conversion
├── ConflictResolver.swift # Conflict resolution strategy
├── SyncMonitor.swift # Account status + sync state observation
└── CloudSyncError.swift # Typed error handling with CKError mappingIf sharing enabled:
Sources/CloudSync/Sharing/
├── ShareManager.swift # CKShare creation and management
└── ShareParticipantView.swift # UICloudSharingController wrapperStep 3: Determine File Location
Check project structure:
- If
Sources/exists ->Sources/CloudSync/ - If
App/exists ->App/CloudSync/ - Otherwise ->
CloudSync/
Step 4: Customize for Project
Adapt templates to match:
- User's model types and property names
- Bundle identifier for CloudKit container ID
- Chosen conflict resolution strategy
- Database scope (private only vs. private + shared)
Step 5: Entitlements Setup
Generate or update entitlements file with required CloudKit capabilities.
Entitlements and Capabilities Setup
Required Xcode Capabilities
- iCloud capability:
- Check "CloudKit"
- Add container:
iCloud.com.<team-identifier>.<app-bundle-id>
- Background Modes (recommended):
- Check "Remote notifications" (for push-based sync triggers)
Required Entitlements
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.icloud-container-identifiers</key>
<array>
<string>iCloud.com.yourcompany.yourapp</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudKit</string>
</array>
</dict>
</plist>CloudKit Dashboard Setup
- Go to CloudKit Dashboard
- Select your container
- Record types are auto-created when you first save a CKRecord of that type during development
- Deploy schema to production before App Store release
- Indexes are required for queryable fields -- add them in the dashboard
Output Format
After generation, provide:
Files Created
Sources/CloudSync/
├── SyncEngine.swift # CKSyncEngine + delegate implementation
├── SyncConfiguration.swift # Container, zone, and scope config
├── RecordMapping.swift # CKRecord <-> model bridging
├── ConflictResolver.swift # Pluggable conflict resolution
├── SyncMonitor.swift # Account status + sync state
├── CloudSyncError.swift # Error types with CKError mapping
└── Sharing/ # (if sharing enabled)
├── ShareManager.swift # CKShare lifecycle
└── ShareParticipantView.swiftIntegration Steps
1. Initialize the sync engine at app launch:
@main
struct MyApp: App {
@State private var syncEngine = SyncEngine()
var body: some Scene {
WindowGroup {
ContentView()
.environment(syncEngine)
.task {
await syncEngine.start()
}
}
}
}2. Send local changes to CloudKit:
// After saving a local model
let recordID = CKRecord.ID(recordName: item.id.uuidString, zoneID: SyncConfiguration.zoneID)
syncEngine.addPendingChange(.saveRecord(recordID))3. Handle incoming changes in your model layer:
The SyncEngine delegate methods automatically call RecordMapping to convert fetched CKRecord objects into your local model types and persist them.
4. Monitor sync status in the UI:
struct SyncStatusView: View {
@Environment(SyncMonitor.self) private var syncMonitor
var body: some View {
HStack {
if syncMonitor.isSyncing {
ProgressView()
Text("Syncing...")
} else if let error = syncMonitor.lastError {
Image(systemName: "exclamationmark.icloud")
Text(error.localizedDescription)
} else {
Image(systemName: "checkmark.icloud")
Text("Up to date")
}
}
}
}Testing
Use a separate CloudKit container for development:
#if DEBUG
let containerID = "iCloud.com.yourcompany.yourapp.dev"
#else
let containerID = "iCloud.com.yourcompany.yourapp"
#endifTest account status handling:
@Test
func handlesNoAccountGracefully() async {
let monitor = SyncMonitor()
await monitor.handleAccountStatus(.noAccount)
#expect(monitor.accountAvailable == false)
#expect(monitor.lastError is CloudSyncError)
}Test conflict resolution:
@Test
func serverWinsConflictResolution() {
let resolver = ConflictResolver(strategy: .serverWins)
let serverRecord = makeCKRecord(title: "Server Version", modifiedAt: .now)
let clientRecord = makeCKRecord(title: "Client Version", modifiedAt: .distantPast)
let resolved = resolver.resolve(server: serverRecord, client: clientRecord)
#expect(resolved["title"] == "Server Version")
}Verification Checklist
After generation, verify:
- App compiles without errors
- Entitlements file contains CloudKit container identifier
- CloudKit container exists in Apple Developer portal
- CKSyncEngine initializes without crash
- Local changes appear as pending record zone changes
- Fetched changes are converted to local models
- Conflict resolution behaves as configured
- Account status changes are observed and surfaced to UI
- (If sharing) CKShare can be created and participants added
- App handles offline gracefully (queues changes)
- App handles "no iCloud account" gracefully
Common Customizations
Adding a New Synced Model Type
- Add
CKRecordfield mapping inRecordMapping.swift - Register the record zone in
SyncConfiguration.swift(if using a separate zone) - Update
nextRecordZoneChangeBatch()to include pending changes for the new type
Switching Conflict Resolution
// Change strategy without touching sync engine code
let resolver = ConflictResolver(strategy: .timestampMerge)
let config = SyncConfiguration(conflictResolver: resolver)Adding Shared Database Support
- Set
databaseScopeto include.sharedinSyncConfiguration - Add
ShareManagerto handleCKSharelifecycle - Wrap
UICloudSharingControllerfor the sharing UI
Troubleshooting
Sync Not Working
- Verify device is signed into iCloud (Settings > Apple Account)
- Check entitlements match the CloudKit container identifier exactly
- Confirm container exists in CloudKit Dashboard
- Look for CKError logs in Console.app -- filter by "CloudKit"
- Ensure
CKSyncEngineis started (not just initialized)
"User Did Not Sign In" Error
CKAccountStatus.noAccount-- prompt user to sign into iCloudCKAccountStatus.restricted-- parental controls or MDM restrictionCKAccountStatus.temporarilyUnavailable-- retry after delay
Schema Deployment
- Development schema changes are automatic
- Production schema must be explicitly deployed from CloudKit Dashboard
- Schema changes in production are additive only (cannot remove fields)
Rate Limiting
- CloudKit has per-user rate limits
CKError.requestRateLimitedincludesretryAfterSecondsinuserInfoCKSyncEnginehandles most retry logic automatically
References
- templates.md -- All code templates for CKSyncEngine infrastructure
- CKSyncEngine Documentation
- CloudKit Overview
- Sharing CloudKit Data
- CloudKit Dashboard
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

