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.invasivecode.com/weblog/advanced-cloudkit-ii | |
| 01. In your project, create a new target and choose a Mac App. | |
| We need to enable CloudKit, as we did with our iOS App. | |
| Select the target that you have just created and follow the same steps, | |
| going to the Capabilities pane and switching on iCloud, and activating | |
| the CloudKit checkbox. This time we want to have access to the iCloud | |
| OS App and select its checkbox. | |
| 02. Now we are ready to create a CKRecord for each of our word definitions. |
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
| 01 - 04. See: https://www.invasivecode.com/weblog/advanced-cloudkit-ii | |
| 05. See: https://www.whatmatrix.com/blog/a-guide-to-cloudkit-how-to-sync-user-data-across-ios-devices/ | |
| 01. In the first method, we create a query with a predicate to fetch the records. | |
| We create a CKQueryOperation and pass it to the second method, along with the operation | |
| queue that we will use to execute it. | |
| func updateWordDefinitionChanges() { |
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.invasivecode.com/weblog/advanced-cloudkit-i | |
| 01. Now when the App launches, instead of directly performing a fetch to CloudKit, what we are going to do first is to check if we have already subscribed to the record changes. If we are subscribed we do not need to do anything else, as we will be notified by CloudKit via remote push notifications. If we are still not subscribed (for example, the first time the user uses the App), before subscribing, we will need to perform a fetch of the record, to be sure that we have the most updated version of the data. Then, we will check if the user has an iCloud account available. In this case, we need an iCloud account to be available, because we are going to save the subscription on CloudKit, and therefore we need write permissions. If the iCloud account is available, we can create a subscription and save it on the public database. The subscription is setup to be fired anytime the record changes. | |
| Let’s add this new method to our AppDelegate and call it w |
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.invasivecode.com/weblog/advanced-cloudkit-i | |
| 01. To access the schema of the database, click on the button CloudKit Dashboard, or | |
| go to https://icloud.developer.apple.com/dashboard. In the CloudKit dashboard, you | |
| can manage the schema of the database, create new Record Types, and add, edit and delete records. | |
| 02. Select Record Types and then click on the + button to create a new record type. | |
| Name the new record type WebServiceSettings. Then, add two fields of type string. | |
| Call them serviceURL and serviceAPIKey. By default, when you create new fields, | |
| indexes are added, to allow sorting, querying and searching on them. In this case, |
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 for example 1:https://www.invasivecode.com/weblog/advanced-cloudkit-i | |
| See for example 2: https://stackoverflow.com/questions/44023936/cloudkit-full-and-complete-error-handling-example | |
| 01. Example 1 | |
| For example, if an operation cannot be performed because the service is unavailable, or you | |
| have exceeded the rate limits, there will be an entry in the dictionary for the key | |
| CKErrorRetryAfterKey. Its value is a NSNumber containing the number of seconds after | |
| you should retry the request: |
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
| 01. Save and Delete Records at the same time | |
| let recordsToSave: [CKRecord] ... | |
| let recordIDsToDelete: [CKRecordID] ... | |
| let saveRecordsOperation = CKModifyRecordsOperation( | |
| recordsToSave: recordsToSave, | |
| recordIDsToDelete: recordIDsToDelete | |
| ) | |
| saveRecordsOperation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, error in | |
| // handle errors here |
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
| 01. Example of saving a single record | |
| let privateDB = CKContainer.defaultContainer().privateCloudDatabase | |
| let record = CKRecord(recordType: “Employee”) | |
| record.setObject(“John”, forKey: “name”) | |
| record.setObject(“Stephens Green West, Dublin 2, Dublin”, forKey: “address”) | |
| privateDB.saveRecord(record) { savedRecord, error in | |
| // handle errors here | |
| } |
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
| 01. Using a query with a predicate to get a list of records. | |
| let privateDB = CKContainer.defaultContainer().privateCloudDatabase | |
| let predicate = NSPredicate(format: "firstName = %@", "John") | |
| let query = CKQuery(recordType: "Artist", predicate: predicate) | |
| privateDB.performQuery(query, inZoneWithID: nil) { records, error in | |
| guard let records = records else { return } | |
| //Use the records.. | |
| } |
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
| CKContainer.defaultContainer().accountStatusWithCompletionHandler{ status, error in | |
| guard status == .Available else { return } | |
| //The user’s iCloud account is available.. | |
| } |
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
| 01. See:https://stackoverflow.com/questions/43553969/handling-cloudkit-errors | |
| 02. See: https://www.whatmatrix.com/blog/a-guide-to-cloudkit-how-to-sync-user-data-across-ios-devices/ | |
| 03. See CloudKit error codes: https://developer.apple.com/documentation/cloudkit/ckerrorcode | |
| 04. https://stackoverflow.com/questions/44023936/cloudkit-full-and-complete-error-handling-example | |
| 01. Helper function to manage CloudKit Errors | |
| import Foundation | |
| import CloudKit |