android - Kotlin Flow - 有没有类似于 LiveData 的 emitSource 的东西?

标签 android kotlin android-livedata kotlin-flow

我有一个函数,它接受一个返回 Flow 的参数函数,

fun <T> resultFlow(
  query: () -> Flow<T>,
  ... other parameters
): Flow<T> {
  return flow {

    val source = query()

    //!!!!    emit(source)   // I want something like emitSource like livedata has

   ...
   }

有没有什么可以让flow可以这样发送的?

最佳答案

如果你想从流中发出所有项目,这正是 emitAll()做:

fun <T> resultFlow(
  query: () -> Flow<T>,
  ... other parameters
): Flow<T> {
  return flow {

    val source = query()

    // Note that this will suspend until all values from the flow are collected.
    emitAll(source)
}

关于android - Kotlin Flow - 有没有类似于 LiveData 的 emitSource 的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68027567/

相关文章:

android - 内存不足加载 imageViews

java - 为什么 Kotlin 中没有并发关键字?

kotlin - 导航组件 : How to clear back stack WHEN NAVIGATING FROM FRAGMENT TO ACTIVITY?

loops - 是否可以使用while循环制作标签?

retrofit2 - 如果 api 检索数据失败,加载指示器不会隐藏,尽管它会在 api 成功检索 Android Paging 库中的数据时隐藏

android - 为什么我需要在 Android 中扩展 Activity?

android - 向上滚动时用动画展开 Recyclerview 项目

androidx 数据绑定(bind)生命周期 NullPointerException

Android:当项目太多时,ListView 隐藏下面的布局

java - 多 Activity 中用 EventBus 替换 LiveData