How to use DispatchGroup in Swift?

Uplift iOS Interview

The Guide is for YOU if
  • You are preparing for an iOS interview and want to improve your skills and knowledge and looking to level up your interview game and land your dream job.
  • You want to gain confidence and ease during iOS interviews by learning expert tips and curated strategies.
  • You want access to a comprehensive list of iOS interview QA to practice and prepare.

DispatchGroup allows you to add a set of tasks and schedule them for asynchronous execution. This behaviour can be helpful when progress can’t be made until all of the specified tasks are complete.


For instance, If you have several long-running tasks to perform and you’d like to run some further logic, you need DispatchGroup. However, You can run each task sequentially, but it is not very efficient.

let group = DispatchGroup()

func downloadImages() {
    // Mimicking the list of image URLs
    for i in 0..<5 {
        group.enter()
        print("Image download start - \(i)")

        // Mimicking the download fucntion
        DispatchQueue.global().async {
            sleep(arc4random() % 5)
            print("Image downloaded - \(i)")
            group.leave()
        }
    }

    group.notify(queue: DispatchQueue.global()) {
        print("All images are downloaded.")
        // ... makeCollage()
    }
}

downloadImages()

//Output
//Image download start - 0
//Image download start - 1
//Image download start - 2
//Image download start - 3
//Image download start - 4
//Image downloaded - 0
//Image downloaded - 4
//Image downloaded - 1
//Image downloaded - 3
//Image downloaded - 2
//All images are downloaded.

In the code example, I have downloaded many images concurrently, followed by the makeCollage function. When all the images are downloaded, notify will be called.

Get ahead of the competition and ace your next iOS interview with expert-curated resources. Check out the collection of SwiftUI and Swift interview questions and answers, behavioral interview tips, and the popular custom layout contents. Plus, go through the new section for Engineering Manager to help you excel in your career!

Join my free Newsletter 🚀


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(). If you do not want to wait for the groups to finish, but instead want to run a function once all the tasks have completed, use the notify function in place of the group.wait()

DispatchGroup wait() key take ways –

  • wait() – blocks the current thread until the group’s tasks have completed.
  • wait(timeout) – blocks the current thread, but after the timeout specified.
  • wait(timeout) – returns an enum that can be used to determine whether the group completed or timed out.


✍️ Written by Ishtiak Ahmed

👉 Follow me on XLinkedIn



Get Ready to Shine: Mastering the iOS Interview




Enjoying the articles? Get the inside scoop by subscribing to my newsletter.

Get access to exclusive iOS development tips, tricks, and insights when you subscribe to my newsletter. You'll also receive links to new articles, app development ideas, and an interview preparation mini book.

If you know someone who would benefit from reading this article, please share it with them.