簡體   English   中英

等待兩個異步完成功能完成,然后再執行下一行代碼

[英]wait for two asynchronous completion functions to finish, before executing next line code

我有兩個功能: func Females_NonChat()func males_NonChat()我想等待它們都完成后再在viewdidload中執行打印語句。 我需要另一個完成處理程序來完成嗎?

這些功能是用於從在線數據庫中請求信息的firebase完成處理程序...

override func viewDidLoad() {
    super.viewDidLoad()
    func Females_NonChat()
    func males_NonChat()

    print("finished executing both asynchronous functions")
}

func Females_NonChat(){
    Anon_Ref.child("Chatting").child("female").observeSingleEventOfType(.Value, withBlock: {(snapshot) in
        if let FemInChatting = snapshot.value as? [String : String] {
            print("executing")
        }
    })
}

func males_NonChat(){
    Anon_Ref.child("Chatting").child("male").observeSingleEventOfType(.Value, withBlock: {(snapshot) in
        print("executing")
    })
}

通常,您將使用調度組,在每個異步方法之前輸入該組,在每個異步方法完成后離開該組,然后在所有“輸入”調用與相應的“離開”調用匹配時設置一個組通知:

override func viewDidLoad() {
    super.viewDidLoad()

    let group = dispatch_group_create()

    dispatch_group_enter(group)
    Females_NonChat() {
        dispatch_group_leave(group)
    }

    dispatch_group_enter(group)
    males_NonChat() {
        dispatch_group_leave(group)
    }

    dispatch_group_notify(group, dispatch_get_main_queue()) { 
        print("finished executing both asynchronous functions")
    }
}

func Females_NonChat(completionHandler: () -> ()) {
    Anon_Ref.child("Chatting").child("female").observeSingleEventOfType(.Value) { snapshot in
        if let FemInChatting = snapshot.value as? [String : String] {
            print("executing")
        }
        completionHandler()
    }
}

func males_NonChat(completionHandler: () -> ()) {
    Anon_Ref.child("Chatting").child("male").observeSingleEventOfType(.Value) { snapshot in
        print("executing")
        completionHandler()
    }
}

這是一個執行兩個異步方法並在兩個方法都完成時打印的示例。

嘗試將此代碼復制到Swift Playground中並運行它。

import Foundation

func doTwoThings() {
    var thing1Done: Bool = false
    var thing2Done: Bool = false

    func done() {
        if thing1Done && thing2Done {
            print("Both things done! at \(getTime())")
        }
    }

    doThing1(completionHandler: {
        thing1Done = true
        done()
    })

    doThing2(completionHandler: {
        thing2Done = true
        done()
    })
}

func doThing1(completionHandler: @escaping () -> Void) {
    print("Starting Thing 1 at \(getTime())")
    Timer.scheduledTimer(withTimeInterval: 3, repeats: false, block: {_ in
        print("Done with Thing 1 at \(getTime())")
        return completionHandler()
    })
}

func doThing2(completionHandler: @escaping () -> Void) {
    print("Starting Thing 2 at \(getTime())")
    Timer.scheduledTimer(withTimeInterval: 5, repeats: false, block: {_ in
        print("Done with Thing 2 at \(getTime())")
        return completionHandler()
    })
}

func getTime() -> String {
    let date = Date()
    let calendar = Calendar.current
    let hour = calendar.component(.hour, from: date)
    let minute = calendar.component(.minute, from: date)
    let second = calendar.component(.second, from: date)
    return "\(hour):\(minute):\(second)"
}

doTwoThings()

輸出:

Starting Thing 1 at 11:48:51
Starting Thing 2 at 11:48:51
Done with Thing 1 at 11:48:54
Done with Thing 2 at 11:48:56
Both things done! at 11:48:56

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM