1

我有一个将数据作为流返回的 API:

suspend fun loadData(...): Flow<List<Items>> 
suspend fun loadDetails(...): Flow<ItemDetails>

当我获取数据时,我需要加载一些项目的详细信息并将结果转换为实时数据:

job = SupervisorJob()
val context = CoroutineScope(job + Dispatchers.Main.immediate).coroutineContext
liveData(context = context) {
  emitSource(
        api.loadData(...)
           .flatMapConcat { items ->
              items.forEach { item -> 
                 if (item is Details){
                    api.loadDetails(item).flatMapConcat{ details ->
                        item.details = details
                    }
                 }
              }
             flow {
               emit(items)
             }
            }
   ) 

这里的问题在完成emit(items)之前调用loadDetails,所以item.details = details更新调用。

如何等待forEach更新所有项目?

4

1 回答 1

3

好吧,我在这里做了一些假设,所以如果我有任何错误,请在评论中纠正我,我会更新我的答案。

一般来说,flatMapConcat如果你不是绝对需要它,不鼓励使用它(它实际上写在函数的文档中)

我假设loadDetails可以简单地表示为:

suspend fun loadDetails(item: Details) = flow<Any>{
    emit(Loading()) // some sort of sealed wrapper class
    val value = someAsyncOperation()
    emit(Success(value))
}

现在我们定义一个简单的辅助函数来获取SuccessloadDetails

suspend fun simplerLoadDetails(item: Details): Any { 
    // this will collect items of the flow until one matches the lambda
    val wrappedSuccessValue = loadDetails(item)
        .first { it is Success } as Success 
    return wrappedSuccessValue.value
}

另一个处理整个列表

suspend fun populateDetails(items: List<Any>) {
    items.forEach {
        if (it is Details) {
            it.details = simplerLoadDetails(it)
        }  
    }

}

此处注意:如果您需要并行使用,这将按顺序处理所有元素

suspend fun populateDetails(items: List<Any>) {
    coroutineScope {
        items.forEach {
            if (it is Details) {
                launch {
                    it.details = simplerLoadDetails(it)
                }  
            }
        }
    }
}

现在对于 发出的每个元素列表loadData,我们需要做的就是调用populateDetails

suspend fun itemsWithDetails(...) = loadData(...)
    .onEach { itemsList ->
        populateDetails(itemsList)
    }

于 2020-07-28T10:52:56.387 回答