How to use DispatchGroup GCD with Swift 3

Wilson Balderrama
1 min readSep 24, 2016

There were some changes regarding GCD with the new version Swift 3.

So I will provide a practical example using DispatchGroup.

Let’s say that we have a method called run which accepts two parameters one is about the number of seconds to be delayed and the another one is a closure that is going be run after the delaying, it would be something like this:

let secondsDelay = 3run(secondsDelay) { // run this after the seconds delay}

The importante thing with run function is that calls its closure asynchronously.

Now let’s say we call run function four times and after the four times are complete we want to be notified in order to do something, and the code should be something like this:

First we create DispatchGroup called group, then before each call to the run function, we call group.enter() in order to keep track that a call to the run function was done.

And every time that every run function completes its delay will call its closure and inside it we call group.leave() to tell GDC that the call is complete.

And only after all the calls to run function are done, group.notify(..) will be invoked.

Source of the run function:

--

--

Wilson Balderrama

I like to run everyday while listening podcasts or audiobooks, read/write at Medium and of course I try to improve my skills as an iOS Developer everyday.