performQuery 跳过 CompletionHandler

performQuery skips CompletionHandler

我的 completionHandler 在我的 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

当我到达底部 "for" 语句时,centerResults 为零。我的目的是阅读我的 Public "Centers" 架构中的所有条目。

在我的仪表板中,我有一个 "Centers" 架构,有 4 Public 条记录。

有什么问题吗?

performQuery 是一个异步调用。所以你可能会在调用 completionHandler 之前到达 for 循环。尝试将底部的代码移动到 completionHandler 内部。并在 completionHandler 的第一行设置调试,看看发生了什么。