我想删除父CKRecord和此记录的所有子项。创建父记录和子记录的方法如下:
func save(){ //创建父记录
让……
解
如果我们要删除子记录,我们必须添加引用 parent record 至 child record 。在上面的情况下,它应该看起来像这样:
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 }) } } }
在这种情况下,删除 parent record make,儿童记录将被自动删除