Giter Club home page Giter Club logo

cloudkitdemo's Introduction

CloudKitDemo

The CloudKit framework provides interfaces for moving data between your app and your iCloud containers. You use CloudKit to store your app’s existing data in the cloud so that the user can access it on multiple devices.

Requirements

Xcode 9+ iOS 11+

Implementation

Here's the link to my blog for implementation of CloudKit and it's usage.

Usage

Create a container to store records.

let container = CKContainer.default().publicCloudDatabase

Create Record

To create a record to CloudKit Database, you also have to set value for a key like UserDefaults in cloudkit.

let record = CKRecord(recordType: "Title")
record.setValue("\(UUID())", forKey: "id")
record.setValue(title, forKey: "title")
container.save(record, completionHandler: { (record, error) in
 })

Fetch Record

To fetch a record from CloudKit Database, you have to use a query of CKQuery type which takes predicate and RecordType as parameter and then you pass that query.

let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "RECORD_TYPE", predicate: predicate)
container.perform(query, inZoneWith:  CKRecordZone.default().zoneID, completionHandler: { (records, error) in
  })

If you want to fetch a single record you can fetch it using CKRecord.Id a unique identifier in CloudKit to distinguish between Records.

container.fetch(withRecordID: id) { record, error in
  }

or

You can use query as well of CKQuery to query records.

let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Title", predicate: predicate)
container.perform(query, inZoneWith:  CKRecordZone.default().zoneID, completionHandler: { (records, error) -> Void in
 })

Delete Record

To delete a record from CloudKit Database. You have to pass in CKRecord.ID a unique identifier in CloudKit to distinguish between Records.

container.delete(withRecordID: record.recordID) { (recordID, error) -> Void in
 }

Update Record

To update a record you have to update the record you have to fetch record using ID and then using the key update a newValue to it.

container.fetch(withRecordID: id) { record, error in
record?.setValue(NEW_TITLE, forKey: "title")
container.save(record, completionHandler: { (record, error) in
 })
}

or

If you have created a unique identifier of string from yourself, you can use this as well!

let predicate = NSPredicate(format: "id == %@", id)
let query = CKQuery(recordType: "Title", predicate: predicate)
container.perform(query, inZoneWith:  CKRecordZone.default().zoneID, completionHandler: { (records, error) -> Void in
 })
}

cloudkitdemo's People

Contributors

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