我可以在 kotlin flow 中使用 async{} 吗?
场景:在 API 调用之后,我得到了一个包含 200 个对象的列表,我需要解析(转换为 UIObject)。 我正在尝试并行处理此列表。 下面是伪代码:
fun getUIObjectListFlow():Flow<List<UIObject>> {
flow<List<UIObject>> {
while (stream.hasNext()) {
val data = stream.getData() // reading from an input stream. Data comes in chunk
val firstHalfDeffered = async(Dispatchers.IO) { /* process first half of the list that data obj contains*/ }
val secondHalfDeffered = async(Dispatchers.IO) { /*process second half of the list that data obj contains */ }
val processedList = firstHalfDeffered.await() + secondHalfDeffered.await() // just pseudo code
emit(processedList)
}
}
}
由于 async{} 需要协程范围(例如: someScope.async{} ),我怎样才能在 flow 中获得范围?有没有其他方法可以做到这一点?
此函数在存储库中,我从 viewmodel 中调用它。
谢谢
最佳答案
(最初问题的原始答案)
正如评论中提到的@broot,您不需要Flow<T>
如果您想要的是生成单个项目(即使该单个项目是一个集合)。
通常,您只需要 suspend
函数(或本例中的暂停代码)而不是返回 Flow
的函数.
现在,无论您是否保留单项流程,您都可以使用 coroutineScope { ... }
挂起函数来定义一个本地范围,您可以从中启动协程。这个函数做了一些事情:
- 它提供了启动子协程的范围
- 它会暂停直到所有子协程完成
- 它根据 block 中的最后一个表达式返回一个值(lambda 的“返回”值)
这是它的样子:
val uiObjects = coroutineScope { //this: CoroutineScope
val list = getDataFromServer()
val firstHalf = async(Dispatchers.IO) { /*process first half of the list */ }
val secondHalf = async(Dispatchers.IO) { /*process second half of the list */ }
// the last expression from the block is what the uiObjects variable gets
firstHalf.await() + secondHalf.await()
}
编辑:鉴于问题更新,这里是一些更新的代码。您仍应使用 coroutineScope
为您的短期协程创建本地范围:
fun getUIObjectListFlow(): Flow<List<UIObject>> = flow<List<UIObject>> {
while (stream.hasNext()) {
val data = stream.getData() // reading from an input stream. Data comes in chunk
val processedList = coroutineScope {
val firstHalfDeffered = async(Dispatchers.IO) { /* process first half of the list that data obj contains*/ }
val secondHalfDeffered = async(Dispatchers.IO) { /*process second half of the list that data obj contains */ }
firstHalfDeffered.await() + secondHalfDeffered.await()
}
emit(processedList)
}
}
关于android - async{} 内部流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67887644/