Is there a cleaner way to do this in Coroutines, possibly using
flatmap()
or some functional operator, without having to introduce Kotlin Flows?
Copy code
viewModelScope.launch {
val api = NetworkService().api
val ordersResponse = api.fetchOrdersCoroutine()
val deliveryItems = mutableListOf<DeliveryItem>()
ordersResponse.orders.forEach { orderId ->
val orderResponse = api.fetchOrderByIdCoroutine(orderId)
deliveryItems.addAll(orderResponse.items)
}
}
m
Matt Rea
04/29/2022, 6:44 PM
map { }
would work here. Doesn't really matter that its in a coroutine
Copy code
val api = NetworkService().api
val deliveryItems = api.fetchOrdersCoroutine().orders.map { order ->
api.fetchOrderByIdCoroutine(order.id)
}