我有這樣的查詢parse.com。 爲什麼numObjects變量具有findObjectsInBackgroundWithBlock內側和功能不同的值離開Swift,parse.com:如何通過查詢傳遞數據
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
}
這是因爲塊在後臺線程上異步運行(查看方法名稱)。由於它是異步的,它會立即返回並在它後面繼續執行代碼。這意味着在上面的代碼塊之前調用第二個'println(numObjects)'。 – Milo