我有两个在 ViewModel 中并行启动的挂起方法:
init {
viewModelScope.launch(Dispatchers.Default) {
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))
}
但是,我想防止_viewState.value = _viewState.value.copy...
在我的两种方法中同时执行这些部分。实现这一目标的最佳方法是什么?