11/29/2017 - 8:18 PM

Swift - CloudKit - Creating a Custom Zone

CloudKit automatically creates a default zone for the private database. However, you can get more functionality if you use a custom zone, most notably, support for fetching incremental record changes.

See: https://www.whatmatrix.com/blog/a-guide-to-cloudkit-how-to-sync-user-data-across-ios-devices/

Since this is a first example of using an operation, here are a couple of general observations:

First, all CloudKit operations have custom completion closures (and many have intermediate closures, 
depending on the operation). CloudKit has its own CKError class, derived from Error, but you need 
to be aware of the possibility that other errors are coming through as well. Finally, one of the 
most important aspects of any operation is the qualityOfService value. Due to network latency, 
airplane mode, and such, CloudKit will internally handle retries and such for operations at a 
qualityOfService of “utility” or lower. Depending on the context, you may wish to assign a higher 
qualityOfService and handle these situations yourself.

Once set up, operations are passed to the CKDatabase object, where they’ll be executed on a 
background thread.

// Create a custom zone to contain our note records. We only have to do this once.
private func createZone(completion: @escaping (Error?) -> Void) {
	let recordZone = CKRecordZone(zoneID: self.zoneID!)
	let operation = CKModifyRecordZonesOperation(recordZonesToSave: [recordZone], recordZoneIDsToDelete: [])
	operation.modifyRecordZonesCompletionBlock = { _, _, error in
		guard error == nil else {
	operation.qualityOfService = .utility
	let container = CKContainer.default()
	let db = container.privateCloudDatabase