我正在嘗試更新CloudKit中的現有記錄,但我正在創建新記錄而不是更改值!這是代碼:如何更新CloudKit中記錄類型的字段?
func pairing(phone: String, ctid: String) {
ctUsers = [CKRecord]()
print("THE PHONE IS: \(phone)\n")
let publicData = CKContainer.default().publicCloudDatabase
let predicate = Predicate(format: "phone == %@", phone)
let query = CKQuery(recordType: "Elder", predicate: predicate)
publicData.perform(query, inZoneWith: nil) { (results: [CKRecord]?, error: NSError?) -> Void in
if error != nil {
print(error?.localizedDescription)
}
if let users = results {
self.ctUsers = users
print("\nHow many users in cloud: \(self.ctUsers.count)\n")
if self.ctUsers.count != 0 {
let user = CKRecord(recordType: "Elder")
user["careTakerId"] = ctid
let publicData = CKContainer.default().publicCloudDatabase
publicData.save(user, completionHandler: { (record: CKRecord?, error: NSError?) in
if error == nil {
DispatchQueue.main.asynchronously(execute: {() -> Void in
print("CARETAKER updated successfully\n")
})
}
else {
print("\nHUEHUEHUEHUE\n")
print(error?.localizedDescription)
}
})
}
else {
print("\nELDER ISN'T IN CLOUD\n")
print(error?.localizedDescription)
}
}
}
}
快速的解釋:首先,我越來越老的電話號碼(每個老人都有不同的電話),並檢查該電話號碼是在雲中。如果是的話,那麼更新這位長輩的careTakerId,特別是!
在這裏你可以看到,在創建這個「NO NAME」記錄,上面記錄保持不變
任何想法?
哦,不介意的語法,我使用的Xcode 8個beta 2個版本 –