我对 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/