Shivam Verma
06/03/2021, 7:41 PMinit {
viewModelScope.launch {
launch { loadTotalCirculation() }
launch { loadMarketPrice() }
}
}
private suspend fun loadTotalCirculation() {
val totalCirculation = bitcoinChartsRepository.getTotalCirculation(5, TimeUnit.HOURS)
_viewState.value = _viewState.value.copy(totalCirculation = chartViewEntityMapper(totalCirculation))
}
private suspend fun loadMarketPrice() {
val marketPrice = bitcoinChartsRepository.getMarketPrice(27, TimeUnit.DAYS)
_viewState.value = _viewState.value.copy(marketPrice = chartViewEntityMapper(marketPrice))
}
However, I would like to prevent the concurrent execution of the _viewState.value = _viewState.value.copy...
parts in both my methods. What's the best way to achieve this ?
These two requests update parts of a stateflow that's collected inside a Fragment. However, often they run concurrently emitting the wrong states. Is there a way to better architect this solution ?Jeff
06/03/2021, 10:32 PM_viewState.value = _viewState.value.copy(
totalCirculation = loadTotalCirculation().await(),
marketPrice = loadMarketPrice().await())
Another option, if you need each to update the UI on their own, would be to do a redux style where they post their results to a channel and you update your _viewState each time the channel emits.Shivam Verma
06/04/2021, 9:40 AMShivam Verma
06/06/2021, 10:37 AMJeff
06/07/2021, 12:03 AM