Code Monkey home page Code Monkey logo

sample-cloudkit-sync-engine's Introduction

CloudKit Samples: CKSyncEngine

Goals

This project demonstrates using CKSyncEngine to sync data in an app.

Prerequisites

  • A Mac with Xcode 15 beta 5 (or later) installed is required to build and test this project.
  • An iOS device running iOS 17 beta 4 or later, or Mac running macOS 14 beta 4 or later, is required to run this app.
  • An active Apple Developer Program membership is needed to create a CloudKit container and sign the app to run on a device.

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.

Setup Instructions

  1. Ensure you are logged into your developer account in Xcode with an active membership.
  2. 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.
  3. Ensure that all devices are logged into the same iCloud account.

Using Your Own iCloud Container

  • Create a new iCloud container through Xcode’s “Signing & Capabilities” tab of the SyncEngine app target.
  • Update the CKContainer in SyncedDatabase.swift with your new iCloud container identifier.

How it Works

  • The main CKSyncEngine integration is contained in SyncedDatabase.swift.
  • On first launch, the app initializes a SyncedDatabase, which syncs a local store with the server.
  • 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.
  • Saving the Contact record triggers a push notification, which tells other devices to fetch the record from the server.
  • When other devices fetch the record, they save them to the local store, and the UI shows the new data.

Example Flow

  1. Run the app on a device or Mac. Latest changes are fetched from the server.
  2. Repeat the above on another device and add a new contact through the UI.
  3. The first device fetches the changes and shows the contact in the UI.

Tests

This project includes a few basic tests for CKSyncEngine integration in 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.

Things To Learn

  • Syncing data with CKSyncEngine.
  • Adding, deleting, and merging remote changes into a local store, and reflecting those changes live in a UI.
  • Testing your integration with CKSyncEngine.

Further Reading

sample-cloudkit-sync-engine's People

Contributors

fehguy avatar madrob avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.