【问题标题】:Zip network requests via Kotlin Coroutine Flow通过 Kotlin Coroutine Flow 压缩网络请求
【发布时间】:2019-10-30 12:49:25
【问题描述】:

我有一个通过 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

但是,如何通过来自 Kotlin Coroutines 的 Flow 压缩请求?

我知道我可以这样做:

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

但我想通过 Flow 来实现!

我知道 Channels,但 Channels.zip() 已被弃用。

【问题讨论】:

    标签: android retrofit kotlin-coroutines


    【解决方案1】:
    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

    【讨论】:

      【解决方案2】:

      对于大多数操作Flow 遵循与普通协程相同的规则,因此要压缩两个单独的请求,您需要应用async concurrency pattern

      实际上,这最终会是这样的:

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

      【讨论】:

      • 感谢您的回答。我认为 Flow Coroutines 可以取代流行的 RX 操作符,但它看起来并不简洁。也许,最好使用 Channels 来完成任务?你觉得怎么样?
      • @kiskae 这里的“结果”是什么?
      • 代码是OPs原RxJava代码的直接翻译,所以是OP提供的一个类。
      猜你喜欢
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      相关资源
      最近更新 更多