我正在尝试使用Kotlin
Flows
. 这是我现在正在尝试的
flowOf(
remoteDataSource.getDataFromCache() // suspending function returning Flow<Data>
.catch { error -> Timber.e(error) },
remoteDataSource.getDataFromServer() // suspending function returning Flow<Data>
).flattenConcat().collect {
Timber.i("Response Received")
}
这里的问题collect
是仅在getDataFromServer
返回时调用。我的期望是我应该从缓存中获取第一个事件,然后在几毫秒后从服务器获取第二个事件。在这种情况下"Response Received"
,会打印两次,但会立即打印一次。
在此其他变体"Response Received"
中,仅在getDataFromServer()
返回后打印一次。
remoteDataSource.getDataFromCache() // suspending function returning Flow<Data>
.catch { error -> Timber.e(error) }
.flatMapConcat {
remoteDataSource.getDataFromServer() // suspending function returning Flow<Data>
}
.collect {
Timber.i("Response Received")
}
我之前使用的是 RxJava Flowable.concat()
,它运行良好。Kotlin Flows 中有什么东西可以模仿这种行为吗?