Search code examples
cloudkit

performQuery skips CompletionHandler


My completionHandler is being skipped over in my performQuery.

let container = CKContainer.defaultContainer()
var publicDB: CKDatabase!

    publicDB = container.publicCloudDatabase
    let query = CKQuery(recordType: "Centers", predicate: NSPredicate(value: true))
    publicDB.performQuery(query, inZoneWithID: nil, completionHandler: { results, error in
        if error != nil
        {
            dispatch_async(dispatch_get_main_queue())
            {
                println("error loading: \(error)")
            }
        }
        else
        {
            self.centerResults = results
        }
    })

    var center = Center()
    for item in centerResults

When I get to that bottom "for" statement, centerResults is nil. My intent is to read all the entries in my Public "Centers" schema.

In my Dashboard, I have a "Centers" schema, with 4 Public records.

What could be wrong?


Solution

  • The performQuery is an asynchronous call. so you will probably reach the for loop before the completionHandler has been called. Try moving the code at the bottom to inside the completionHandler. And set a debug on the first line in the completionHandler to see what's going on.