Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CloudKit -How to Cancel a Save/Upload Operation that's in Progress

Is there a way to cancel a CloudKit save or upload operation that’s in progress?

This isn’t a time-out situation. I notice videos take some time to upload and the user might decide to dismiss the vc. I would like to cancel the current upload if they do that.

let database = CKContainer.default().publicCloudDatabase

var operation: CKModifyRecordsOperation?

func save(videoURL: URL) {

    let record = CKRecord(recordType: "MyType")
    let fileURL = URL(fileURLWithPath: videoURL.path)
    let asset = CKAsset(fileURL: fileURL)
    record["videoAsset"] = asset

    database.save(record) { (record, err) in
    }

    // or

    operation = CKModifyRecordsOperation(recordsToSave: [record], recordIDsToDelete: nil)
    operation?.savePolicy = .allKeys
    operation?.modifyRecordsCompletionBlock = { (savedRecordIDs, deletedRecordsIDs, error) in
    }

    database.add(operation!)
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Since you have a reference to CKModifyRecordsOperation, you can call cancel.

operation?.cancel()

If you look up the doc for CKModifyRecordsOperation, it says the following.

If you assign a completion handler to the completionBlock property of
the operation, CloudKit calls it after the operation executes and
returns the results. Use the completion handler to perform any
housekeeping tasks for the operation, but don’t use it to process the
results of the operation. The completion handler you provide should
manage any failures of the operation, whether due to an error or an
explicit cancellation.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading