如何删除 CKRecord

How to delete a CKRecord

如何在客户端以编程方式从我的数据库中删除 CKRecord

for record in records {
    if recordNumber < 13 {
        let moodRecord:CKRecord = record as! CKRecord
        self.moodArray.append(moodRecord.objectForKey("Color") as! String)
    }
    else if recordNumber > 13 {
        // DELETE RECORD HERE
        record.delete(CKRecord)
        recordNumber--
    }
    recordNumber++
}

这不是您在 CloudKit 中删除记录的方式。以下是我在应用程序中删除记录的方法:

let operation = CKModifyRecordsOperation(recordsToSave: nil, recordIDsToDelete: [record.recordID])
operation.savePolicy = .AllKeys
operation.modifyRecordsCompletionBlock = { added, deleted, error in
    if error != nil {
        println(error) // print error if any
    } else {
        // no errors, all set!
    }
}
CKContainer.defaultContainer().publicCloudDatabase.addOperation(operation)

您可以使用

delete(withRecordID:completionHandler:)

apple docs

objc 版本:

[aDatabase deleteRecordWithID:rec.recordID completionHandler:^(CKRecordID * _Nullable recordID, NSError * _Nullable error) {
    ;
 }];
let database = CKContainer.default().publicCloudDatabase // or privateCloudDatabase

let recordID = someRecord.recordID
    
database.delete(withRecordID: recordID) { (ckRecordID, error) in

    if let error = error {
        print(error.localizedDescription)
        return
    }
            
    guard let id = ckRecordID else {
        return
    }

    print(id)
}