How to use "fetchWithRecordID" for auto-generated ID in CloudKit Dashboard?
I am new to CloudKit and iOS development. I have manually added some entries to the CloudKit Dashboard and now I want to retrieve them by their IDs that were automatically generated. And then I want to display the record value in the UILabel for a static cell of the tableview. Has two questions: 1). I am getting an error in xCode when using an id from control panel (the error says "expected", "delimiter"); and 2). I cannot find examples of entering record value in UILabel in static cell of tableview. (especially for the fast one). Any help is greatly appreciated!
Here's my code:
override func viewDidLoad() {
super.viewDidLoad()
publicDB.fetchRecordWithID (f7080e7a-f8c3-4db6-b8ee-642a011a6762) { record, error in
if error != nil {
println("there was an error \(error)")
} else {
// this is my UILabel in a static tableview. The record only attribute is //called "subCategory"
plasticOneValue.text = record["subCategory"]
}
}
}
UPDATE: ALTERNATIVELY - I tried this code and the build was successful, but the console said it had an internal error and the UILabel is still empty ...... Any suggestions on what I am doing wrong with the above code or this code ?
override func viewDidLoad() {
super.viewDidLoad()
// I created a new CKRecord ID Object and provided the existing ID in dashboard and //it fixed the error about requiring a "," separator
var plasticOneID = CKRecordID(recordName: "f7080e7a-f8c3-4db6-b8ee-642a011a6762")
publicDB.fetchRecordWithID (plasticOneID) { record, error in
if error != nil {
println("there was an error \(error)")
} else {
self.plasticOne.text = (record.objectForKey("subCategory") as String)
}
}
}
source to share
These are two different problems.
- when asking for an ID, you need to ask for the CKRecordID as in the second example.
- When accessing the user interface, you must do so in the main queue.
So, the code will be something like this:
publicDB.fetchRecordWithID(CKRecordID(recordName: "f7080e7a-f8c3-4db6-b8ee-642a011a6762"), completionHandler: {record, error in
if error != nil {
println("there was an error \(error)")
} else {
NSOperationQueue.mainQueue().addOperationWithBlock {
self.plasticOne.text = (record.objectForKey("subCategory") as String)
}
}
})
source to share