[英]In function call, why parse statement get executed atlast in SWIFT?
我正在開始SWIFT。 我嘗試了以下代碼來檢查函數調用的工作方式。 如我們所願,函數調用工作正常。 但是,在PARSE中,該順序在parse語句中不起作用。 當所有函數結束時,解析語句得到執行圖集。 如何解決這個問題。 如果我運行這段代碼,我將得到類似的輸出,
我的輸出:
START
FIRST CLASS TOP
FIRST CLASS BOTTOM
SECOND CLASS
THIRD CLASS
END
WELCOME TO PARSE // WHY THIS LINE IS PRINTING LAST??
但是,我需要這樣的輸出,
所需的輸出:
START
FIRST CLASS TOP
WELCOME TO PARSE //I NEED THIS LINE TO BE EXECUTE IN ORDER.
FIRST CLASS BOTTOM
SECOND CLASS
THIRD CLASS
END
我的編碼低於。 請檢查並指導我。
class ViewController: UIViewController {
let one_1 = class_1()
let second_2 = class_2()
let third_3 = class_3()
override func viewDidLoad() {
super.viewDidLoad()
println("START")
one_1.one()
second_2.second()
third_3.third()
println("END")
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//CLASS_1
class class_1 {
var par_query = PFQuery(className: "story")
func one() {
println("FIRST CLASS TOP")
par_query.findObjectsInBackgroundWithBlock({(NSArray objects, NSError error) in
if (error != nil) {
NSLog("error " + error.localizedDescription)
}
else {
println("WELCOME TO PARSE")
}//ELSE ENDING
}) //PARSE ENDING
println("FIRST CLASS BOTTOM")
}
}
//CLASS_2
class class_2 {
func second() {
println("SECOND CLASS")
}
}
//CLASS_3
class class_3 {
func third() {
println("THIRD CLASS")
}
}
它與parse.com無關,它之所以如此,是因為findObjectsInBackgroundWithBlock
是異步執行的。 您可以在這里閱讀有關內容。
更新:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
//Do some time comsuming calculation
//And when it's done
dispatch_async(dispatch_get_main_queue()) {
//Update the UI with your results
}
}
更新2
讓我這樣說:如果在異步閉合中打印任何內容,例如“ WELCOME TO PARSE”,則無法確定何時執行。 我將用*標記,您的當前代碼可以在“ WELCOME TO PARSE”消息中出現:
開始
頭等艙
一等艙底
*
第二課
*
第三類
*
結束
*
如果要打印所需的確切行,則可以做兩件事:
將“一流的底部”放在您的異步塊中,然后
asych塊中的class_2().second() class_3().third()
,因此將在執行塊后調用。 但是,我不建議這樣做,僅用於示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.