Lukas Lechner
08/19/2020, 12:33 PMCoroutineExceptionHandler
in a viewModelScope
when e.g. initialising an Android ViewModel
? I want that exceptions by every Coroutine that are then started in this scope are then handled by the handler.Gabriel Feo
08/19/2020, 1:31 PMGabriel Feo
08/19/2020, 1:33 PMChristopher Elías
08/20/2020, 2:40 PMabstract class BaseViewModel : ViewModel() {
private val mapperExceptionHandler = CoroutineExceptionHandler { _, throwable ->
setErrorFromMapperPresentation(throwable.message)
}
val presentationMapperContext : CoroutineContext = <http://Dispatchers.IO|Dispatchers.IO> + mapperExceptionHandler
And use it like this:
private val _ranking = MutableLiveData<SellRanking>()
val ranking : LiveData<RankingModel> = _ranking.switchMap {
liveData(presentationMapperContext) {
emit(mapper.rankingDomainToPresentation(it))
}
}