How to use DispatchGroup in Swift?

Uplift iOS Interview

"Uplift iOS Interview" is a comprehensive guide to help aspiring iOS developers soar to new heights in their careers. This book is an indispensable tool for anyone looking to crack the iOS interview and impress their future employers with their technical prowess. With in-depth coverage of Swift, AutoLayout, SwiftUI, Multithreading, Memory management so on and so forth, this book is a treasure trove of knowledge for anyone looking to uplift their iOS development career.

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.

Rev Up Your iOS Skills: Take a Dynamic Learning Journey
iOS Career Boost

iOS Career Boost is the ultimate learning journey to elevate your iOS development career through a dynamic blend of visual learning, handy cheat sheets, coding practice materials, and expertly curated tips and tricks


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. Your email address will only be used for the purpose of sending the newsletter and will not be shared with third parties or advertisers. Rest assured that we value your privacy and will not spam your inbox.


Connect with me on

Twitter and LinkedIn and don't hesitate to reach out with any questions about this post. Thank you for reading.

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