Skip to content

How to Fix iOS CloudKit Sync Issues — iCloud Not Syncing

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix iOS CloudKit Sync Issues. We cover key concepts, practical examples, and best practices.

The Problem

CloudKit data is not syncing across devices:

Records saved on one device do not appear on another.

Quick Fix

Step 1: Check iCloud account status

CKContainer.default().accountStatus { status, error in
    switch status {
    case .available:
        print("iCloud account available")
    case .noAccount:
        print("No iCloud account")
    case .restricted:
        print("iCloud restricted (parental controls)")
    case .couldNotDetermine:
        print("Could not determine")
    @unknown default:
        break
    }
}

Step 2: Enable CloudKit capability

Target > Signing & Capabilities > + Capability > iCloud > CloudKit service

Ensure the CloudKit container is created.

Step 3: Use CKSubscription for push updates

let subscription = CKQuerySubscription(
    recordType: "Note",
    predicate: NSPredicate(value: true),
    options: .firesOnRecordCreation
)

let notification = CKSubscription.NotificationInfo()
notification.shouldSendContentAvailable = true
subscription.notificationInfo = notification

CKContainer.default().privateCloudDatabase.save(subscription) { _, error in
    if let error = error {
        print("Subscription error: \(error)")
    }
}

Step 4: Handle sync in AppDelegate

func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    let dict = userInfo as! [String: NSObject]
    CKContainer.default().privateCloudDatabase.fetchAllSubscriptions { subscriptions, error in
        completionHandler(.newData)
    }
}

Step 5: Fix conflict resolution

let operation = CKModifyRecordsOperation(recordsToSave: records, recordIDsToDelete: nil)
operation.savePolicy = .changedKeys  // Server wins on conflicts

Prevention

  • Always check accountStatus before performing CloudKit operations.
  • Use CKSubscription for real-time sync.
  • Handle server-side conflict resolution with .changedKeys or .allKeys save policy.

Common Mistakes with cloudkit sync

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

These mistakes appear frequently in real-world IOS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between private and public CloudKit databases?

Private databases are per-user and not shared. Public databases are shared across all users of the app.

Why does CloudKit not sync immediately?

CloudKit syncs when the app is in the foreground or receives a push notification. Use CKSubscription with firesOnRecordCreation for immediate updates.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro