Giter Site home page Giter Site logo

sample-cloudkit-privatedb's Introduction

CloudKit Samples: Private Database

Goals

This buildable (and testable) Xcode project demonstrates a simple use of the CloudKit Private Database. It allows you easily try out reading from and writing to a Private Database for a user in your own container on CloudKit servers.

Prerequisites

Setup Steps

  1. Clone this sample code repository
  2. Open PrivateDatabase.xcodeproj in Xcode
  3. In the General tab of PrivateDatabase Target in Xcode, set your own Bundle Identifier
  4. In the Accounts section of Xcode Preferences, sign into your developer account (in Xcode) if needed
  5. In the Signing & Capabilities tab of PrivateDatabase Target in Xcode, choose your account's Team
  6. In the Signing & Capabilities tab of PrivateDatabaseTests Target in Xcode, choose your account's Team
  7. In the Signing & Capabilities tab of PrivateDatabase Target in Xcode, choose existing iCloud container (or press "+" to create a new container)
  8. Update the containerIdentifier property in Config.swift with your iCloud container name
  9. Launch a Simulator (for example, via the Xcode menu in the menu bar) and ensure the Simulator is logged into an iCloud account in Settings
  10. Test the app in the Simulator (from the Product menu in Xcode)
  11. Run the app in the Simulator (from the Product menu in Xcode)

How It Works

  • Upon launch, the app reads a single record from the CloudKit server.

  • Specifically, this record resides in the Default Zone of the currently signed-in iCloud user's Private Database in the app's CloudKit Container.

  • The record is of type "Person". The "Person" record type, as defined in the CloudKit Container's Schema by the app's developer, has a single custom string field called "name".

  • The specific CloudKit record the app reads has a well-known record ID of "lastPerson". (This well-known ID is hardcoded in the app.)

  • The app's UI displays the name of the last person to write their name into the "name" field of this record on the server.

  • When the user of the app enters their own name into a text field in the UI, the app writes the user's name into the "name" field of this same CloudKit Record and saves it back to the CloudKit Server.

  • Subsequent launches of this app from this device (or other devices) will show this user's name until another user's name is written into the "name" field of the same CloudKit Record.

Things To Learn

  • A working Xcode project that interacts with the CloudKit server

  • Some basic data flows between CloudKit and a SwiftUI View

  • Reading from and writing to a CloudKit Private Database

  • Writing a CloudKit record using CKModifyRecordsOperation

  • Overriding the default savePolicy when writing

  • Fetching an explicit record by ID using the fetch(withRecordID:) convenience method on CKDatabase

  • Some basic error trapping of CKError errors, including those embedded in partialFailure

  • Some basic testing using XCTest

Note on Swift Concurrency

This project uses Swift concurrency APIs. A prior completionHandler-based implementation has been tagged pre-async.

Further Reading

sample-cloudkit-privatedb's People

Contributors

sjrmanning 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.