Pablo
12/16/2024, 3:08 PM.launchIn(viewModelScope)
it does?
Doesn't collect:
init {
viewModelScope.launch {
busDataRepository.getBusStops()
.map<List<BusStop>, BusStopsDBScreenUiState> { busStops ->
BusStopsDBScreenUiState.Success(busStops, Res.string.bus_stops_db_filled)
}
.onEach { newState -> _uiState.emit(newState)}
.catch { throwable -> _uiState.emit(BusStopsDBScreenUiState.Error(throwable))}
}
}
Do collect:
init {
busDataRepository.getBusStops()
.map<List<BusStop>, BusStopsDBScreenUiState> { busStops ->
BusStopsDBScreenUiState.Success(busStops, Res.string.bus_stops_db_filled)
}
.onEach { newState -> _uiState.emit(newState)}
.catch { throwable -> _uiState.emit(BusStopsDBScreenUiState.Error(throwable))}
.launchIn(viewModelScope)
}
Filip Wiesner
12/16/2024, 3:35 PMonEach
is not terminal operator. You have to call .collect { }
Chrimaeon
12/16/2024, 4:27 PMmattinger
12/16/2024, 4:49 PMPablo
12/16/2024, 5:33 PMalvr
12/16/2024, 6:03 PMlaunchIn
is a terminal operator that internally executes collect
.
https://github.com/kotlin/kotlinx.coroutines/tree/master/kotlinx-coroutines-core/common/src/flow/terminal/Collect.kt#L45mattinger
12/16/2024, 8:51 PMPablo
12/16/2024, 10:31 PMPablo
12/16/2024, 10:31 PMmattinger
12/17/2024, 2:40 PM