Long-running tasks in parallel with Kotlin Flow

Amit Shekhar
Outcome School
Published in
3 min readOct 12, 2022

I am Amit Shekhar, Co-Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

In this blog, we will learn about how to run long tasks in parallel with Kotlin Flow in Android.

This article was originally published at Outcome School.

I will be using the following project for the implementation part. The project follows a basic MVVM Architecture for simplicity. You can find the complete code for the implementation mentioned in this blog in the project itself.

GitHub Project: Learn Kotlin Flow

For running the long tasks in parallel, we will be needing the zip operator of Kotlin Flow. So, we need to understand what is a zip operator of Kotlin Flow.

What is a zip operator of 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 understand the marble diagram with the example code:

val flowOne = flowOf(1, 2, 3)
val flowTwo = flowOf("A", "B", "C")
flowOne.zip(flowTwo) { intValue, stringValue ->
"$intValue$stringValue"
}.collect {
println(it)
}

The output will be the following:

1A
2B
3C

Long-running tasks in parallel

A real use case in Android: When we want to run two tasks in parallel and want the results of both tasks in a single callback when both tasks are completed.

Let’s see the code implementation.

Suppose we have two long-running tasks.

Long Running Task One:

private fun doLongRunningTaskOne(): Flow<String> {
return flow {
// your code for doing a long running task
// Added delay to simulate
delay(5000)
emit("One")
}
}

Long Running Task Two:

private fun doLongRunningTaskTwo(): Flow<String> {
return flow {
// your code for doing a long running task
// Added delay to simulate
delay(5000)
emit("Two")
}
}

Now, using the zip operator:

fun startLongRunningTask() {
viewModelScope.launch {
doLongRunningTaskOne()
.zip(doLongRunningTaskTwo()) { resultOne, resultTwo ->
return@zip resultOne + resultTwo
}
.flowOn(Dispatchers.Default)
.catch { e ->
// handle exception
}
.collect {
// result
}
}
}

The output will be the following:

OneTwo

Here, as we have used a zip operator, it runs both tasks in parallel and gives the results of both tasks in a single callback when both tasks are completed.

By zipping two flow collections using the Zip operator, both tasks run in parallel. And we get the result when both get completed. In this way, we get the results of both the flow collections at a time.

Advantages of Zip Operator of Kotlin Flow:

  • Run tasks in parallel.
  • Return the results of tasks in a single callback when all the tasks are completed.

This way we can use the Zip Operator of Flow in Kotlin for running the tasks in parallel.

Master Kotlin Coroutines from here: Mastering Kotlin Coroutines

That’s it for now.

Thanks

Amit Shekhar

Co-Founder @ Outcome School

You can connect with me on:

Read all of our high-quality blogs here.

--

--

Amit Shekhar
Outcome School

Co-Founder @ Outcome School | Coder | Teacher | Mentor | Open Source | IIT 2010-14 | Android | Machine Learning | Backend