CloudKit的使用
Retrieve existing records
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: recordType, predicate: predicate)
CKContainer.default().publicCloudDatabase.perform(query, inZoneWith: nil) { records, error in
}
Create a new record
let record = CKRecord(recordType: "RecordType")
record.setValue("Some data", forKey: "key")
CKContainer.default().publicCloudDatabase.save(record) { savedRecord, error in
}
Update the record
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
}
}
Remove the record
let recordId = CKRecordID(recordName: recordId)
CKContainer.default().publicCloudDatabase.delete(withRecordID: recordId) { deletedRecordId, error in
}