iCloud에서 데이터를 가져오는 것 뿐이지 거의 Core Data와 사용법이 비슷하다.
데이터 모델은 CloudKit Console에서 손쉽게 만들면 된다.
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: recordType, predicate: predicate)
CKContainer.default().publicCloudDatabase.perform(query, inZoneWith: nil) { records, error in
}
let record = CKRecord(recordType: "RecordType")
record.setValue("Some data", forKey: "key")
CKContainer.default().publicCloudDatabase.save(record) { savedRecord, error in
}
let recordId = CKRecordID(recordName: "RecordType")
CKContainer.default().publicCloudDatabase.fetch(withRecordID: recordId) { updatedRecord, error in
if error != nil {
return
}
updatedRecord.setObject("Some data", forKey: "key")
CKContainer.default().publicCloudDatabase.save(updatedRecord) { savedRecord, error in
}
}
let recordId = CKRecordID(recordName: recordId)
CKContainer.default().publicCloudDatabase.delete(withRecordID: recordId) { deletedRecordId, error in
}