我有一个通过 RxJava 压缩两个网络请求的代码:
Single.zip(repository.requestDate(), repository.requestTime()) {
date, time -> Result(date, time)
}
代表着repository.requestDate()
/repository.requestTime()
回报Single<T>
如果我想使用协程,我需要将请求更改为:
@GET('link/date')
suspend fun requestDate() : Date
@GET('link/time')
suspend fun requestTime() : Time
But, 如何通过 Flow 从 Kotlin 协程压缩请求?
我知道我可以这样做:
coroutineScope {
val date = repository.requestDate()
val time = repository.requestTime()
Result(date, time)
}
但我想通过 Flow 来做到这一点!
我了解频道,但是Channels.zip()
已弃用。
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 https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/zip.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)