
This video is only available to subscribers. Start a subscription today to get access to this and 472 other videos.
CKRecord Upload Progress
This episode is part of a series: Hello CloudKit.
1. Hello Cloud Kit - Part 1 10 min |
2. Hello Cloud Kit - Part 2 10 min |
3. CloudKit Querying 12 min |
4. CloudKit References 7 min |
5. Fetching and Saving References 15 min |
6. Working with Images 15 min |
7. Fetching Paged Images 8 min |
8. CKRecord Upload Progress 6 min |
9. Isolating CloudKit from your Controllers 16 min |
10. Extracting CKRecordWrapper 7 min |
11. CloudKit Notes Manager 11 min |
12. CloudKit Notes Manager Continued 14 min |
Episode Links
- Source Code
- CKModifyRecordsOperation Class Reference - We use this class to get more control about how records are saved.
Migrating to CKModifyRecordsOperation
The previous CKDatabase.save()
function is a simple convenience wrapper around CKModifyRecordsOperation
, and as such it doesn't give us all the control and functionality.
To track progress, we’ll have to migrate over to this new class.
let modifyOp = CKModifyRecordsOperation(
recordsToSave: [photo.record],
recordIDsToDelete: nil
)
Setting the Blocks for Progress and Completion
This is also how you batch saves together in a single call, which saves the round-trip cost for saving many things at once.
We have to fill out a few blocks on this operation to get it working:
This block will be called when the photo(s) have finished uploading...
modifyOp.modifyRecordsCompletionBlock = { records, _, error in
if let e = error {
print("Error saving photo: \(e)")
} else {
self.prepend(photo: photo)
}
And this block will be called for per-record completion percentage...
modifyOp.perRecordProgressBlock = { record, progress in
DispatchQueue.main.async {
self.progressView.setProgress(Float(progress), animated: true)
}
}
Make sure you are on the main thread before updating any UI element, since these are called from a different thread.
Starting the Operation
Like the rest of CloudKit, we'll have to add this operation to a database to get it running:
database.add(modifyOp)
Build and run and you’ll be able to upload a new photo and see progress during the upload!