@@ -3,10 +3,7 @@ package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase1
33import androidx.lifecycle.viewModelScope
44import com.lukaslechner.coroutineusecasesonandroid.base.BaseViewModel
55import com.lukaslechner.coroutineusecasesonandroid.mock.MockApi
6- import kotlinx.coroutines.CancellationException
7- import kotlinx.coroutines.CoroutineExceptionHandler
8- import kotlinx.coroutines.async
9- import kotlinx.coroutines.launch
6+ import kotlinx.coroutines.*
107import timber.log.Timber
118
129class ExceptionHandlingViewModel (
@@ -40,34 +37,33 @@ class ExceptionHandlingViewModel(
4037 uiState.value = UiState .Loading
4138 viewModelScope.launch {
4239
40+ supervisorScope {
41+ val oreoFeaturesDeferred = async { api.getAndroidVersionFeatures(27 ) }
42+ val pieFeaturesDeferred = async { api.getAndroidVersionFeatures(28 ) }
43+ val android10FeaturesDeferred = async { api.getAndroidVersionFeatures(29 ) }
4344
44- // supervisorScope {
45- val oreoFeaturesDeferred = async { api.getAndroidVersionFeatures(27 ) }
46- val pieFeaturesDeferred = async { api.getAndroidVersionFeatures(28 ) }
47- val android10FeaturesDeferred = async { api.getAndroidVersionFeatures(29 ) }
48-
49- val versionFeatures = listOf (
50- oreoFeaturesDeferred,
51- pieFeaturesDeferred,
52- android10FeaturesDeferred
53- ).mapNotNull {
54- try {
55- it.await()
56- } catch (exception: Exception ) {
57- // We have to re-throw cancellation exceptions so that
58- // our Coroutine gets cancelled immediately.
59- // Otherwise, the CancellationException is ignored
60- // and the Coroutine keeps running until it reaches the next
61- // suspension point.
62- if (exception is CancellationException ) {
63- throw exception
45+ val versionFeatures = listOf (
46+ oreoFeaturesDeferred,
47+ pieFeaturesDeferred,
48+ android10FeaturesDeferred
49+ ).mapNotNull {
50+ try {
51+ it.await()
52+ } catch (exception: Exception ) {
53+ // We have to re-throw cancellation exceptions so that
54+ // our Coroutine gets cancelled immediately.
55+ // Otherwise, the CancellationException is ignored
56+ // and the Coroutine keeps running until it reaches the next
57+ // suspension point.
58+ if (exception is CancellationException ) {
59+ throw exception
60+ }
61+ Timber .e(" Error loading feature data!" )
62+ null
6463 }
65- Timber .e(" Error loading feature data!" )
66- null
6764 }
65+ uiState.value = UiState .Success (versionFeatures)
6866 }
69- uiState.value = UiState .Success (versionFeatures)
70- // }
7167 }
7268 }
7369}
0 commit comments