Make Multiple API Calls Using Dispatch Group

Diayan Siat
3 min readJan 1, 2023

--

Photo by Daria Nepriakhina 🇺🇦 on Unsplash

API calls are an essential part of app development. Almost every app you know and use today performs some kind of API/Network calls from a remote server.

Performing a network call can be very easy, but what happens when you have to make multiple network calls on a single screen? This is a common practice on e-commerce and social media apps where you see different data being displayed on the same screen. And I recently found myself in your situation.

For the sake of your beloved users’ experience, you do not want some parts of the screen to load and display data whiles the rest of the screen is still empty. This will be a complete disaster in terms of usability. In this tutorial, I will show you how to do this like a pro using the iOS concurency tool, DispatchGroup.

DispatchGroup is a powerful tool for performing asynchronous tasks on iOS. They allow you to synchronize the completion of tasks and can be used to make multiple API calls in a controlled manner.

To use a DispatchGroup, you first have to create a group and then use a pair of “enter” and “leave” commands available in the group to indicate entering and leaving the DispatchGroup when the task is completed.

Assuming you had to make three API calls together, here is how you can use DispatchGroup to achieve this:

  1. In your Swift file, import Dispatch, UIKit or Foundation frameworks:
import Foundation
import UIKit
import Dispatch

Remember, you don’t need to import all three, just one of them will do.

2. Next, create a dispatch group by initializing the DispatchGroup class like this:

let dispatchGroup = DispatchGroup()

3. Make your API calls within the dispatch group by enclosing them in the enter() and leave() methods of the group.

dispatchGroup.enter()
makeAPICall1 { result in
//process results
dispatchGroup.leave()
}

dispatchGroup.enter()
makeAPICall2 { result in
//process results
dispatchGroup.leave()
}

dispatchGroup.enter()
makeAPICall3 { result in
//process results
dispatchGroup.leave()
}

4. Once you have made all the network calls, you can use the notify(queue:execute:) to specify a block of code that should be executed when all the tasks in the group have completed.

dispatchGroup.notify(queue: .main) {
// at this point all tasks are completed, do whatever you wish
}

5. If you want to execute a certain block of code only after the tasks in the dispatch group is complete, you can use the wait() method.

dispatchGroup.wait()
//everything down here will only execute after the dispatch group is completed

Using dispatch groups is a simple and effective way to synchronize the completion of multiple asynchronous tasks in iOS. It allows you to make multiple API calls and perform any necessary processing once all the calls have completed.

If you found this article helpful, please give it a clap and follow for more.

In the next several articles, I will be doing a series on conccurency on iOS focusing on the Grand Central Dispatch. Follow me if you are interested this topic and would like to learn more.

--

--

Diayan Siat

Android | iOS Engineer. I write about Mobile Engineering, Life Experience and Personal Development.