Kotlin Flow Zip Operator for Parallel Multiple Network Calls

In this tutorial, we are going to learn about the Kotlin Flow Zip Operator and how to make the multiple network calls in parallel using it. This tutorial will also help you in doing any type of background tasks in parallel using Kotlin Flow Zip Operator.
Before starting, for your information, this blog post is a part of the series that we are writing on Flow APIs in Kotlin Coroutines.
Resources to get started with Kotlin Flow:
- What is Flow in Kotlin?
- Understanding Terminal Operators in Kotlin Flow
- Creating Flow Using Flow Builder in Kotlin
- Exception Handling in Kotlin Flow
- Instant Search Using Kotlin Flow Operators
- Learn Kotlin Flow in Android by Examples
- Kotlin Flow Retry Operator with Exponential Backoff Delay
Let's get started.
First, let's understand what is a zip operator in Kotlin Flow.
What is a zip operator in Kotlin Flow?
Zip Operator is an operator that combines the emissions of two flow collections together via a specified function and emits single items for each combination based on the results of this function.

Let's see the code:
val flowInt = flowOf(1, 2, 3)
val flowString = flowOf("A", "B", "C")
flowInt.zip(flowString) { intValue, stringValue ->
"$intValue$stringValue"
}.collect {
Log.d(TAG, it)
}
The result will be:
1A
2B
3C
Parallel Multiple Network Calls
A real use case in Android: When we want to make two network calls in parallel and want the results of both the network calls in a single callback when both the network calls are completed.
I will be using this project for the implementation part. You can find the complete code for the implementation mentioned in this blog in the project itself.
We will take the example of
which is present in the project.ParallelNetworkCallsViewModel
Basically, this
is a ViewModel that is associated with ParallelNetworkCallsViewModel
which triggers the ParallelNetworkCallsActivity
ViewModel
to fetch the list of users to render into the UI. The
, then asks the data layer for the list of users using the ParallelNetworkCallsViewModel
ApiHelper
. The ViewModel makes the two network calls in parallel which are as
and getUsers
. getMoreUsers
So, here we have two flow
of network calls:
getUsers
getMoreUsers
As you can see below, the ViewModel uses the Kotlin Coroutines and LiveData. Also, notice that the zip operator is used in the below code.
class ParallelNetworkCallsViewModel(
private val apiHelper: ApiHelper,
private val dbHelper: DatabaseHelper
) : ViewModel() {
private val users = MutableLiveData<Resource<List<ApiUser>>>()
init {
fetchUsers()
}
private fun fetchUsers() {
viewModelScope.launch {
users.postValue(Resource.loading(null))
apiHelper.getUsers()
.zip(apiHelper.getMoreUsers()) { usersFromApi, moreUsersFromApi ->
val allUsersFromApi = mutableListOf<ApiUser>()
allUsersFromApi.addAll(usersFromApi)
allUsersFromApi.addAll(moreUsersFromApi)
return@zip allUsersFromApi
}
.flowOn(Dispatchers.Default)
.catch { e ->
users.postValue(Resource.error(e.toString(), null))
}
.collect {
users.postValue(Resource.success(it))
}
}
}
fun getUsers(): LiveData<Resource<List<ApiUser>>> {
return users
}
}
Here, as we have used a zip operator, it makes both the network calls in parallel and gives the results of both the network calls in a single callback when both the network calls are completed.
By zipping two flow collections using the Zip operator, both the network calls run in parallel. And we get the result when both finish. In this way, we get the results of both the flow collections at a time.
Advantages of Zip Operator of Flow
- Run both the tasks in parallel.
- Return the results of two tasks in a single callback when both the tasks are completed.
This way we can use Zip Operator of Flow to solve the interesting problem.
That's it for now.
Happy Learning :)
Show your love by sharing this blog with your fellow developers.
Also, Let’s become friends on Twitter, Linkedin, Github, Quora, and Facebook.