在执行下一行代码之前等待两个异步完成函数完成

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

我有两个函数:func Females_NonChat()func males_NonChat() 我想等他们两个都完成后再执行viewdidload中的print语句。我需要另一个完成处理程序来完成吗?

使用的这些函数是用于从在线数据库请求信息的 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")
    })
}

一般会使用调度组,在每个异步方法前入组,每个异步方法完成后离开组,然后设置所有"enter"调用匹配时的组通知对应"leave"次调用:

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 游乐场并 运行 它。

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