Reputation: 1
Would really value some support.
I am designing an tvOS app which displays certain CloudKit content in a tableView, the data is different depending on the current date.
Each calendar date correspondents to the data within that Type.
e.g. RecordType "A13032019" relates to 13th March 2019.
I generate the date in the correct format using a func fired within ViewDidLoad (generateDate():
*
func generateDate() {
let formatter = DateFormatter() //2016-12-08 03:37:22 +0000 formatter.dateFormat = "ddMMyyyy" let now = Date() let dateString = formatter.string(from:now) NSLog("%@", dateString) let generateOperationalDate = ("A\(dateString)") print(generateOperationalDate) }
I then try to use generateOperationalData to run in the below CKQuery:
func queryDatabase() {
let query = CKQuery(recordType: "\(generateDate())", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil)) let sort = NSSortDescriptor(key: "trainDepartureTime", ascending: true) query.sortDescriptors = [sort] database.perform(query, inZoneWith: nil) { (records, _) in guard let records = records else { return } let sortedRecords = records
When I try to run this it throws a Thread Error with reason "* Terminating app due to uncaught exception 'CKException', reason: 'recordType can not be empty' * "
So it appears to me that the queryDatabase function is running before the generateDate function, however I have tried delaying the queryDatabase function and this still throws the same error!
Is there anyway I can generate the date (via generateDate) before the queryDatabase function runs?
Thanks
Upvotes: 0
Views: 117
Reputation: 426
You want to write generateDate() so it returns a String. Then, inside queryDatabase(), call generateDate() so you're guaranteed to have a value to pass to "recordType".
I also condensed the calls a little bit. Hope this helps.
func generateDate() -> String {
let formatter = DateFormatter()
formatter.dateFormat = "ddMMyyyy"
let dateString = formatter.string(from:Date())
return "A\(dateString)"
}
func queryDatabase() {
// this will make sure you have a String value
let type = generateDate()
// now you can pass it
let query = CKQuery(recordType: type, predicate: NSPredicate(value: true))
//...
}
Upvotes: 0