等待两个异步完成函数完成,然后再执行下一行代码

时间:2016-08-26 21:57:01

标签: ios swift firebase-realtime-database grand-central-dispatch completionhandler

我有两个功能: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")
    })
}

2 个答案:

答案 0 :(得分:6)

通常,您使用调度组,在每个异步方法之前输入组,在每个异步方法完成后离开组,然后在所有"输入"之后设置组通知。呼叫由相应的"匹配"匹配。调用:

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

答案 1 :(得分:1)

这是一个示例,它执行两个异步方法并在两个方法都完成时打印。

尝试将此代码复制到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
相关问题