Async Wait: вызовAndroid

Форум для тех, кто программирует под Android
Anonymous
Async Wait: вызов

Сообщение Anonymous »

Этот код взят с Udemy-Course, по которому я сейчас следую: < /p>

Код: Выделить всё

fun getWeatherData() {
viewModelScope.launch(exceptionHandler) {
uiState = try {
val currentWeather = async { getCurrentData() }.await()
val forecastWeather = async { getForecastData() }.await()

WeatherHomeUiState.Success(Weather(currentWeather, forecastWeather))
} catch (e: Exception) {
Log.e("WeatherHomeViewModel", e.message.toString(),)
WeatherHomeUiState.Error
}
}
}
< /code>
 Правильно ли вызываться сразу же ожидание отложенного? Разве это не приводит к последовательному выполнению двух функций «GetCurrentData» и «GetForeCastData»?val currentWeather = async { getCurrentData() }
val forecastWeather = async { getForecastData() }

WeatherHomeUiState.Success(Weather(currentWeather.await(), forecastWeather.await()))
сначала запустите оба функцию, поэтому они уже работают, а затем ожидают.

Подробнее здесь: https://stackoverflow.com/questions/796 ... ly-correct

Вернуться в «Android»