elvisfromsouth
elvisfromsouth

Reputation: 145

Zip network requests via Kotlin Coroutine Flow

I have a code which zips two network requests via RxJava:

Single.zip(repository.requestDate(), repository.requestTime()) {
  date, time -> Result(date, time)
}

it means that repository.requestDate()/repository.requestTime() returns Single<T>

If I want to use Coroutines I need to change requests to:

@GET('link/date')
suspend fun requestDate() : Date

@GET('link/time')
suspend fun requestTime() : Time

But, How can I zip requests via Flow from Kotlin Coroutines?

I know that I can do it like this:

coroutineScope {
   val date = repository.requestDate()
   val time = repository.requestTime()
   Result(date, time)
}

But I want to do it via Flow!

I know about Channels, but Channels.zip() is deprecated.

Upvotes: 14

Views: 17107

Answers (3)

stephennn
stephennn

Reputation: 1

You can use the zip or combine operator.

Upvotes: 0

Kiskae
Kiskae

Reputation: 25573

For most operations Flow follows the same rules as normal co-routines, so to zip two separate requests you need to apply the async concurrency pattern.

In practise this will end up looking like this:

flow {
    emit(coroutineScope/withContext(SomeDispatcher) {
        val date = async { repository.requestDate() }
        val time = async { repository.requestTime() }
        Result(date.await(), time.await())
    })
}

Upvotes: 7

AaronJ
AaronJ

Reputation: 714

val dateFlow = flowOf(repository.requestDate())
val timeFlow = flowOf(repository.requestTime())
val zippedFlow = dateFlow.zip(timeFlow) { date, time -> Result(date, time) }

https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/zip.html

Upvotes: 20

Related Questions