Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
897 views
in Technique[技术] by (71.8m points)

swift - Call completion block when two other completion blocks have been called

I have a function doEverything that takes a completion block. It calls two other functions, doAlpha and doBeta which both have completion blocks. These two functions should run asynchronously. I want to call doEverything's completion block after both of the other functions have called their completion blocks.

Currently, it looks like this:

func doEverything(completion: @escaping (success) -> ())) {
    var alphaSuccess = false
    var betaSuccess = false

    doAlpha { success in
        alphaSuccess = success
    }

    doBeta { success in
        betaSuccess = success
    }

    // We need to wait here
    completion(alphaSuccess && betaSuccess)
}

doAlpha and doBeta should run at the same time and, once they've both completed, the completion block should be called with the result of alpha and beta.

I've read into dispatch groups and barriers but I'm not sure which is the most appropriate, how they both introduce new scope (with regards to the two variables I'm using) and how I should implement that.

Many thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Grand Central Dispatch (GCD) is a pretty good choice of what are you trying to do here, you can achieve this by using DispatchQueue and DispatchGroup, as follows:

Swift 3:

func doEverything(completion: @escaping () -> ()) {
    let queue = DispatchQueue(label: "reverseDomain", attributes: .concurrent, target: .main)
    let group = DispatchGroup()

    group.enter()
    queue.async (group: group) {
        print("do alpha")

        group.leave()
    }

    group.enter()
    queue.async (group: group) {
            print("do beta")

        group.leave()
    }

    group.notify(queue: DispatchQueue.main) {
        completion()
    }
}

Or, you can implement it this way (which I find more readable):

func doEverything(completion: @escaping () -> ()) {
    let queue = DispatchQueue(label: "reverseDomain", attributes: .concurrent, target: .main)
    let group = DispatchGroup()

    queue.async (group: group) {
        print("do alpha")
    }

    queue.async (group: group) {
        print("do beta")
    }

    group.notify(queue: DispatchQueue.main) {
        completion()
    }
}

Note that I removed the success flag from the completion closure.

At this case, "do beta" (the execution of the second queue.async) won't be executed until "do alpha" (the execution of the first queue.async) finished, and that's because queue target is .main. If you want to let both of queue.async work concurrently, there is no need to create an extra queue, the same queue should does the work, by replacing:

let queue = DispatchQueue(label: "reverseDomain", attributes: .concurrent, target: .main)

with:

let queue = DispatchQueue(label: "reverseDomain", attributes: .concurrent)

Now, the system will control over how both of queue.async tasks should work concurrently (and obviously, group.notify will be executed after the tow of the tasks finish).

Hope this helped.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...