I have an API that return data as flows:
suspend fun loadData(...): Flow<List<Items>>
suspend fun loadDetails(...): Flow<ItemDetails>
When I get data I need to load details for few items and convert result to live data:
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)
}
}
)
the issue here that emit(items) called before loadDetails completed, so item.details = details newer called.
How to wait forEach updates all items?