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