|
| 1 | +# CloudKit Samples: CKSyncEngine |
| 2 | + |
| 3 | +### Goals |
| 4 | + |
| 5 | +This project demonstrates using `CKSyncEngine` to sync data in an app. |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +* A Mac with [Xcode 15](https://developer.apple.com/xcode/) (or later) installed is required to build and test this project. |
| 10 | +* An iOS device running iOS 17 or later, or Mac running macOS 14 or later, is required to run this app. |
| 11 | +* An active [Apple Developer Program membership](https://developer.apple.com/support/compare-memberships/) is needed to create a CloudKit container and sign the app to run on a device. |
| 12 | + |
| 13 | +**Note**: `CKSyncEngine` relies on remote notifications in order to sync properly. Simulators cannot register for remote push notifications, so running this sample on a real device or Mac is required for this app to properly sync. |
| 14 | + |
| 15 | +### Setup Instructions |
| 16 | + |
| 17 | +1. Ensure you are logged into your developer account in Xcode with an active membership. |
| 18 | +1. In the “Signing & Capabilities” tab of the SyncEngine target, ensure your team is selected in the Signing section, and there is a valid container selected under the “iCloud” section. |
| 19 | +1. Ensure that all devices are logged into the same iCloud account. |
| 20 | + |
| 21 | +#### Using Your Own iCloud Container |
| 22 | + |
| 23 | +* Create a new iCloud container through Xcode’s “Signing & Capabilities” tab of the SyncEngine app target. |
| 24 | +* Update the `CKContainer` in [SyncedDatabase.swift](SyncEngine/SyncedDatabase.swift) with your new iCloud container identifier. |
| 25 | + |
| 26 | +### How it Works |
| 27 | + |
| 28 | +* The main `CKSyncEngine` integration is contained in [SyncedDatabase.swift](SyncEngine/SyncedDatabase.swift). |
| 29 | +* On first launch, the app initializes a `SyncedDatabase`, which syncs a local store with the server. |
| 30 | +* The app’s main UI displays a list of Contacts. When the user adds a new Contact through the UI, this contact is saved to a local store and to the server. |
| 31 | +* Saving the Contact record triggers a push notification, which tells other devices to fetch the record from the server. |
| 32 | +* When other devices fetch the record, they save them to the local store, and the UI shows the new data. |
| 33 | + |
| 34 | +### Example Flow |
| 35 | + |
| 36 | +1. Run the app on a device or Mac. Latest changes are fetched from the server. |
| 37 | +1. Repeat the above on another device and add a new contact through the UI. |
| 38 | +1. The first device fetches the changes and shows the contact in the UI. |
| 39 | + |
| 40 | +### Tests |
| 41 | + |
| 42 | +This project includes a few basic tests for `CKSyncEngine` integration in [SyncTests.swift](Tests/SyncTests.swift). This shows one possible way to test your CloudKit sync code by simulating multiple devices syncing back and forth. The test suite only exercises a few basic scenarios, and there are many more complex scenarios to test in your own application. |
| 43 | + |
| 44 | +### Things To Learn |
| 45 | + |
| 46 | +* Syncing data with `CKSyncEngine`. |
| 47 | +* Adding, deleting, and merging remote changes into a local store, and reflecting those changes live in a UI. |
| 48 | +* Testing your integration with `CKSyncEngine`. |
| 49 | + |
| 50 | +### Further Reading |
| 51 | + |
| 52 | +* [CKSyncEngine Documentation](https://developer.apple.com/documentation/cloudkit/cksyncengine) |
0 commit comments