Apple Sample: CloudKit Core Data Sync

repository·main·Indexed 42 days ago

https://github.com/apple/sample-cloudkit-coredatasync

A sample project demonstrating the integration of Core Data with CloudKit using NSPersistentCloudKitContainer to achieve automatic data synchronization across devices via a user's private CloudKit database.

Tokens
631
Snippets
0
Records
4
Agent score
2%

What's inside CloudKit Core Data Sync

  1. How NSPersistentCloudKitContainer works in this project

    main

    This project uses NSPersistentCloudKitContainer to mirror a local Core Data store to a user's private CloudKit database.

    Core Components

    • Model: The CoreDataSync model defines a Contact entity with a name (String) and a photo (UIImage Transformable).
    • Stack: The PersistenceController class initializes the NSPersistentCloudKitContainer using the CoreDataSync model.
    • UI Integration: The SwiftUI interface uses the @FetchRequest property wrapper to observe and display Contact objects from the local store.
    • Sync Mechanism: Local changes (additions or deletions via NSManagedObjectContext) are automatically sent to the remote CloudKit private database. Remote changes are fetched via background push notifications and merged into the local store.
  2. Setup CoreDataSync with your own iCloud container

    main

    To use a custom iCloud container instead of the default one provided in the sample, follow these steps:

    1. Configure Signing & Capabilities: In Xcode, select the CoreDataSync target. In the "Signing & Capabilities" tab, ensure your developer team is selected and create/select a new iCloud container.
    2. Update Configuration: Update the containerIdentifier property in CoreDataSync/App/Config.swift with your new iCloud container ID.
    3. Account Sync: Ensure both the physical device and the simulator are logged into the same iCloud account to facilitate data mirroring.
  3. Prerequisites for running CloudKit Core Data sync

    main

    To build, run, and test this project, you need:

    • Xcode 12+: Required for building the project.
    • iOS Device: A physical device is required to receive CloudKit change notifications via remote push notifications, as simulators do not support this.
    • Apple Developer Program Membership: Required to create a CloudKit container and sign the application for device installation.
    • Matching iCloud Accounts: The device and simulator must be logged into the same iCloud account to see synchronized data.
  4. Understanding the sync flow between device and simulator

    main

    Because simulators cannot receive remote push notifications, the synchronization flow behaves as follows:

    1. Device to Simulator: If you add a contact on a physical device, the simulator will not automatically update because it cannot receive the push notification. To see the changes, you must restart the app on the simulator; NSPersistentCloudKitContainer will then perform a sync upon initialization.
    2. Simulator to Device: If you add a contact on a simulator, the physical device will receive a background push notification, automatically process the change, and update its local store to match.