Skip to content

Commit 1e733df

Browse files
Fix crash in ExceptionHandlingViewModel.kt
1 parent 116d2f7 commit 1e733df

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

.idea/misc.xml

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase13/ExceptionHandlingViewModel.kt

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase1
33
import androidx.lifecycle.viewModelScope
44
import com.lukaslechner.coroutineusecasesonandroid.base.BaseViewModel
55
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.*
107
import timber.log.Timber
118

129
class 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

Comments
 (0)