Apple CloudKit Sync Engine Sample Implementation

repository·main·Indexed 42 days ago

https://github.com/apple/sample-cloudkit-sync-engine

A sample project demonstrating how to use CKSyncEngine to synchronize local data with a CloudKit server. It provides a practical example of managing a local store that stays in sync with remote changes via push notifications, including a test suite for simulating multi-device synchronization.

Tokens
554
Snippets
0
Records
4
Agent score
4%

What's inside CloudKit Sync Engine Sample

  1. How CKSyncEngine synchronization works

    main

    The synchronization lifecycle in this project follows this pattern:

    1. Initialization: On first launch, the app initializes a SyncedDatabase (located in SyncEngine/SyncedDatabase.swift), which synchronizes a local store with the CloudKit server.
    2. Local Changes: When a user performs an action (e.g., adding a Contact), the record is saved to both the local store and the server.
    3. Remote Notification: Saving a record to the server triggers a push notification.
    4. Remote Sync: The push notification instructs other devices to fetch the new records from the server. Once fetched, these records are saved to the local store, and the UI updates to reflect the new data.
  2. Testing CKSyncEngine integration

    main
    This project provides a sample test suite in Tests/SyncTests.swift. It demonstrates how to test CKSyncEngine integration by simulating multiple devices syncing data back and forth. While this suite only covers basic scenarios, it serves as a template for testing more complex synchronization logic in your own application.
  3. Setup and configure your iCloud container

    main

    To run this sample with your own CloudKit container, follow these steps:

    1. Xcode Configuration: In the "Signing & Capabilities" tab of the SyncEngine target, select your developer team in the Signing section and ensure a valid container is selected under the "iCloud" section.
    2. Container Creation: If you need a new container, create it via the "Signing & Capabilities" tab of the SyncEngine app target.
    3. Code Update: Update the CKContainer identifier in SyncEngine/SyncedDatabase.swift to match your new container identifier.
    4. Account Sync: Ensure all testing devices are logged into the same iCloud account.
  4. Prerequisites for using CKSyncEngine

    main

    To build, run, and test this project, you must meet the following requirements:

    • Xcode: Version 15 beta 5 or later.
    • Hardware: An iOS device running iOS 17 beta 4 or later, or a Mac running macOS 14 beta 4 or later.
    • Account: An active Apple Developer Program membership to create a CloudKit container and sign the app.
    • Connectivity: Because CKSyncEngine relies on remote push notifications for proper synchronization, simulators cannot be used. You must run the app on a real device or a Mac to test syncing functionality.