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

对着背影说爱祢 提交于 2021-02-18 03:40:50

问题


I have two functions: func Females_NonChat() and func males_NonChat() I want to wait for both of them to finish before executing the print statement in viewdidload. Do I need another completion handler to accomplish that?

Those functions used are firebase completion handlers for requesting information from the online database...

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")
    })
}

回答1:


Generally you'd use a dispatch group, enter the group before each asynchronous method, leave the group upon completion of each asynchronous method, and then set up a group notification when all "enter" calls are matched by corresponding "leave" calls:

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()
    }
}



回答2:


Here's an example that executes two async methods and prints when both are finished.

Try copying this code into a Swift Playground and running it.

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()

Output:

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


来源:https://stackoverflow.com/questions/39175156/wait-for-two-asynchronous-completion-functions-to-finish-before-executing-next

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!