Dispatch Groups in Swift 3

Swift 3 brought many changes, including a much needed syntax revamp for Grand Central Dispatch, Apple’s multithreading framework. If you’re familiar with threading, it’s a breeze to use - DispatchQueue.main.async and away you go. In this post we’ll explore a class called DispatchGroup, which isn’t so often used, but can be incredibly helpful.

What does a Dispatch Group do?

Let’s say you’ve got several long running tasks to perform. After all of them have finished, you’d like to run some further logic. You could run each task in a sequential fashion, but that isn’t so efficient - you’d really like the former tasks to run concurrently. DispatchGroup enables you to do exactly this. Let me provide some solid examples:

Great, so how do I use it?

Let’s start with a simple example.

let dispatchGroup = DispatchGroup()

longRunningFunction { dispatchGroup.leave() }

longRunningFunctionTwo { dispatchGroup.leave() }

dispatchGroup.notify(queue: .main) {
    print("Both functions complete 👍")

In the above, both long running functions will perform concurrently, followed by the print statement, which will execute on the main thread. Each call to enter() must be matched later on with a call to leave(), after which the group will call the closure provided to notify(). We must call at least one enter prior to calling notify, so that the group has something blocking it - without this, the notify closure would run before we call enter(). Finally, notice how we specify only .main for the queue? Here, you can provide any queue object you wish (.main is really DispatchQueue.main, not some sort of enum).

A few handy tricks

The above model should provide a great starting point, however DispatchGroup has a few other tricks up its sleeve:

And there you have it - DispatchGroup in a nutshell. Happy (asynchronous) coding.