PiterPan
PiterPan

Reputation: 1802

How to delete CKRecord and all references records in CloudKit

I want to delete parent CKRecord and all children of this record. Method which creates parent record and children record looks like that:

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)
}

and now I would like to delete parent record only by code. The Child record should be deleted automatically because of CKReferenceAction.deleteSelf. Function "delete" looks like this:

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)
        }
    }
}

When I use this delete method, my parent record is deleting from CloudKit database, but child record still exist and I don't know why. Any suggestions?

Upvotes: 0

Views: 1284

Answers (1)

PiterPan
PiterPan

Reputation: 1802

SOLUTION

If we want to delete child record, we have to add reference of parent record to child record. In case above, it should looks like that:

    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
                })
            }
        }
    }

In this case, deleting parent record makes, children record will be deleted automatically

Upvotes: 1

Related Questions