Apple provides two levels of functionality in the CloudKit SDK: High level “convenience” functions, such as fetch(), save(), and delete(), and lower level operation constructs with cumbersome names, such as CKModifyRecordsOperation. The convenience API is much more accessible, while the operation approach can be a bit intimidating. However, Apple strongly urges developers to use the operations rather than the convenience methods.
See: https://www.whatmatrix.com/blog/a-guide-to-cloudkit-how-to-sync-user-data-across-ios-devices/
The CloudKitNoteDatabase Singleton
CloudKit operations provide superior control over the details of how CloudKit does its work and,
perhaps more importantly, really force the developer to think carefully about network behaviors
central to everything CloudKit does. For these reasons, I am using the operations in these code
examples.
Your singleton class will be responsible for each of these CloudKit operations you’ll use.
In fact, in a sense, you’re recreating the convenience APIs. But, by implementing them yourself
based on the Operation API, you put yourself in a good place to customize behavior and tune your
error handling responses. For example, if you want to extend this app to handle multiple Notes
rather than just one, you could do so more readily (and with higher resulting performance) than
if you’d just used Apple’s convenience APIs.
import CloudKit
public protocol CloudKitNoteDatabaseDelegate {
func cloudKitNoteRecordChanged(record: CKRecord)
}
public class CloudKitNoteDatabase {
static let shared = CloudKitNoteDatabase()
private init() {
let zone = CKRecordZone(zoneName: "note-zone")
zoneID = zone.zoneID
}
public var delegate: CloudKitNoteDatabaseDelegate?
public var zoneID: CKRecordZoneID?
// ...
}