Last active
November 29, 2017 20:28
-
-
Save jazzedge/d84714d8a7f41ca6543a408cfa7a57e1 to your computer and use it in GitHub Desktop.
In this example, we will solve the case of having to update a setting of the App, like the server URL and its API key.
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, | |
| we can uncheck all of them as we are only going to perform direct fetches of a record | |
| based on its record name. | |
| 03. Now, select Default Zone, chose WebServiceSettings from the popup menu, and click on | |
| the New Record button to add a new record. In the Record Name field, type a unique | |
| identifier. We will use that identifier to fetch the record from the App. By default, | |
| the dashboard gives you one like 99cae8ed-274e-4f56-87d2-83b27180c3fb, but you can use | |
| any name record you want, as long as it is unique across this database. I am going to | |
| use awesomeWebService for this example. Now, type a URL and an API key in the record | |
| fields, and click on the Save button. | |
| 04. Let’s go back to Xcode. In the AppDelegate.swift, add this method and then call it when | |
| your App launches: | |
| func updateWebServiceSettings() { | |
| let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase | |
| let recordID = CKRecordID(recordName: "awesomeWebService") | |
| publicDatabase.fetchRecordWithID(recordID) { (record: CKRecord?, error: NSError?) -> Void in | |
| guard error == nil else { | |
| return | |
| } | |
| // Update the local copy of the settings | |
| if let localRecord = record, | |
| let urlString = localRecord["serviceURL"] as? String, | |
| let apiKey = localRecord["serviceAPIKey"] as? String { | |
| self.updateSettingsWithServiceURL(NSURL(string: urlString)!, serviceApiKey:apiKey ) | |
| } | |
| } | |
| } | |
| func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
| updateWebServiceSettings() | |
| return true | |
| } | |
| 05. As you can see in the above source code, we are just getting a reference to the | |
| public database. Then we create a recordId with the name of the record we are interested | |
| in (remember that this id is unique). And finally, we tell the public database to perform | |
| a fetch of that record. If the fetch succeeds, we get back an instance of the CKRecord | |
| class with the values of its fields. At this point, you should update your local copy | |
| of the data with the new values. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment