简体   繁体   English

Kotlin Flow——有没有类似LiveData的emitSource的?

[英]Kotlin Flow - is there anything similar to LiveData's emitSource?

I have a function that takes a parameter function returning a Flow,我有一个 function 参数 function 返回一个流,

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

   ...
   }

Is there anything that can be done to flow to be able to be sent like this?有没有什么可以让flow可以这样发送的?

If you want to emit all items from a Flow, that's exactly what emitAll() does:如果您想从 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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM