我想删除父记录CKRecord
以及此记录的所有子项。
创建父记录和子记录的方法如下:
func save() {
//Creating parent record
let parentRecord = CKRecord(recordType: "ParentRecord")
parentRecord["name"] = "BOO" as CKRecordValue
//Creating child record
let childRecord = CKRecord(recordType: "ChildRecord")
childRecord["name"] = "FOO" as CKRecordValue
let reference = CKReference(record: childRecord, action: CKReferenceAction.deleteSelf)
//ADDING Reference to parent record
parentRecord["referenceObject"] = reference as CKRecordValue
let operation = CKModifyRecordsOperation(recordsToSave: [parentRecord, childRecord], recordIDsToDelete: nil)
//([CKRecord]?, [CKRecordID]?, Error?) -> Void)
operation.modifyRecordsCompletionBlock = { records, recordIDs, error in
if error != nil {
print(error.debugDescription)
}
}
operation.queuePriority = .high
operation.qualityOfService = .background
let operationQueue = OperationQueue()
operationQueue.addOperations([operation], waitUntilFinished: false)
}
现在我想只通过代码删除父记录。由于CKReferenceAction.deleteSelf
,应自动删除子记录。
功能"删除"看起来像这样:
func delete(recordName: String) {
let privateDB = CKContainer.default().privateCloudDatabase
let recordID = CKRecordID(recordName: recordName)
privateDB.delete(withRecordID: recordID) { (id, error) in
if error != nil {
print(error.debugDescription)
}
}
}
当我使用此delete
方法时,我的父记录正在从CloudKit
数据库中删除,但子记录仍然存在且我不知道原因。有什么建议吗?
答案 0 :(得分:0)
解
如果我们要删除子记录,我们必须将//clean up animation first
this.setState({stopAnimation: true}, () => {
this.props.navigation.goBack()
})
的引用添加到parent record
。在上面的情况下,它应该是这样的:
child record
在这种情况下,删除 let privateDB = CKContainer.default().privateCloudDatabase
let parentRecord = CKRecord(recordType: "ParentRecord")
parentRecord["name"] = "FOO" as CKRecordValue
let childRecord = CKRecord(recordType: "ChildRecord")
childRecord["name"] = "BOO" as CKRecordValue
privateDB.save(parentRecord) { (record: CKRecord?, error: Error?) in
if error == nil {
if let responseRecord = record {
let reference = CKReference(record: responseRecord, action: CKReferenceAction.deleteSelf)
childRecord["referenceObject"] = reference as CKRecordValue
privateDB.save(childRecord, completionHandler: { (record, error) in
// DO SOMETHIG
})
}
}
}
会自动删除子记录