ios - swift ,parse.com : how to pass data from query

标签 ios parse-platform swift

我对 parse.com 有这样的查询。 为什么numObjects变量在findObjectsInBackgroundWithBlock内部和函数退出时有不同的值

func searchUserInParse () -> Int {

    var numObjects : Int = 0 // the num return objects from query

    var query = PFQuery(className:"Bets")
    query.whereKey("user", equalTo: "Bob")
    query.findObjectsInBackgroundWithBlock {
        (objects: AnyObject[]!, error: NSError!) -> Void in
        if !error {
            numObjects = objects.count
            println(numObjects) // at this point the value = 1
        } else {
            // Log details of the failure
            NSLog("Error: %@ %@", error, error.userInfo)
        }
    }
    println(numObjects) // at this point the value = 0

    return numObjects
}

最佳答案

不要使用异步运行的findObjectsInBackgroundWithBlock,而是尝试使用同步运行的findObjects:

//Set up query...
var objects = query.findObjects()
numObjects = objects.count
println(numObjects)

然后在运行你的函数时,这样做:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) {
    //Search users
    searchUserInParse()

    dispatch_async(dispatch_get_main_queue()) {
        //Show number of objects etc.
    }
}

关于ios - swift ,parse.com : how to pass data from query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24474873/

相关文章:

ios - NSLayoutConstraints 不引用 View 工作

swift - 关于将数组与 State 属性一起使用的问题

ios - NSURLSession 不发送 HTTPAdditionalHeaders 的问题

android - 检测来自移动应用程序的 http 请求

ios - 架构 x86_64 的 Xcode 9 undefined symbol

ios - 移动网络解析iOS SDK Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."

ios - 刷新 TableView 除了拉动刷新

ios - 解析服务器不支持的 URL

ios - SKPolyline在iOS上不显示空白

iOS 从 Swift 类/对象重新加载 UITableView