@@ -3,10 +3,7 @@ package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase1
3
3
import androidx.lifecycle.viewModelScope
4
4
import com.lukaslechner.coroutineusecasesonandroid.base.BaseViewModel
5
5
import 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.*
10
7
import timber.log.Timber
11
8
12
9
class ExceptionHandlingViewModel (
@@ -40,34 +37,33 @@ class ExceptionHandlingViewModel(
40
37
uiState.value = UiState .Loading
41
38
viewModelScope.launch {
42
39
40
+ supervisorScope {
41
+ val oreoFeaturesDeferred = async { api.getAndroidVersionFeatures(27 ) }
42
+ val pieFeaturesDeferred = async { api.getAndroidVersionFeatures(28 ) }
43
+ val android10FeaturesDeferred = async { api.getAndroidVersionFeatures(29 ) }
43
44
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
64
63
}
65
- Timber .e(" Error loading feature data!" )
66
- null
67
64
}
65
+ uiState.value = UiState .Success (versionFeatures)
68
66
}
69
- uiState.value = UiState .Success (versionFeatures)
70
- // }
71
67
}
72
68
}
73
69
}
0 commit comments