How to Call Multiple APIs With Dispatch Group

Nalin Porwal
3 min readJun 29, 2021

Most of the time, during development we come across requirements when we want to call two or more different APIs with different parameters in a single screen to just populate data

In this post, I have explained how to use DispatchGroup to elegantly overcome this challenge and what are its other use cases. Let’s understand it with an example

Let’s have a look at a Food Delivery Application.

Here is the link to my GitHub repo:

https://github.com/NukeNalin/FoodDeliveryDispatchGroupDemo.git

You can download it and check out the HomeViewModel.swift file for better understanding throughout this tutorial.

On the home screen, we need to get three different types of data from three different APIs. In this situation, we often use Nested Closures in which we call the First API. Then after we get a response from the First API, we call the Second API, and so on.

Here is an example of it:

This is one way of doing it but it slowly converts into call-back hell 🔥🔥.

Under this condition, we can use DispatchGroup to make our code more readable and optimized. You can use DispatchGroup where you want to complete a bunch of tasks. Which are not interdependent. Once all the tasks are completed, you can notify the user by some alert and refresh UI or anything you want. DispatchGroup has a function to notify after all tasks. By using this function, you can notify the main thread or global (Background) thread

Let us get familiar with the syntax of DispatchGroup:

First, you need to create an object of DispatchGroup. The second thing is to Enter the function as shown below.

This is a mandatory step. In simple terms, when DispatchGroup executes the task, you will call the Enter function before you write a task.

Finally, when your task is complete, make sure you call the Leave function at the end of the task explicitly just like you called for the Enter function. Leave Function indicates DispatchGroup that the task is completed.

We want to call three different APIs for the home page, first is TopPicks API, second is Cuisines API and the last one is Restaurant API

The syntax will look likes this:

Now, let’s create the DispatchGroup object.

We are going to call the Enter function and then, call our first API. Next, call the Leave function at completion of the first API. Now, before calling the second API, make sure you call Enter function and you must call the Leave function after completion of the second API.

You have to follow the same process for the third API.

At last, you have to call notify function. In my case, I am notifying the main thread to populate data in UI. You can perform one task in notify function closure as per requirement.

Now, let’s call the APIs with Call Back Approach and DispatchGroup and then compare the time taken to complete tasks by both the approaches.

As you can see, the time in DispatchGroup is very less as compared to Call Back Approach. DispatchGroup takes only 2 seconds to complete the tasks, on other hand Call Back Approach tasks take 6 seconds.

And that’s how we can Call Multiple APIs With the Dispatch Group.

Hope your concepts have been cleared. Thanks for reading my tutorial. For more such articles, you can follow. I will be sharing new weekly content, stay tuned!

Keep learning and keep coding!

--

--

Nalin Porwal

I am Nalin Porwal, a thinker, fun lover, problem solver and a friendly guy. An established and acknowledged iOS Developer