Last active
November 29, 2017 20:18
-
-
Save jazzedge/9e6f448d032c0e773f7f7b3a9bbb5b03 to your computer and use it in GitHub Desktop.
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, Appl…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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? | |
| // ... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment