-
Notifications
You must be signed in to change notification settings - Fork 444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
usecase4/VariableAmountOfNetworkRequestsViewModel#performNetworkRequestsConcurrently crashes on exception #8
Comments
Just did some more investigating. I see that the exception gets caught by the |
I read your post about coroutine exceptions, which confirmed that the Any exception thrown within the The following covers all exception cases: fun performNetworkRequestsConcurrently() {
uiState.value = UiState.Loading
viewModelScope.launch(CoroutineExceptionHandler {
_, throwable ->
Timber.v(throwable)
uiState.value = UiState.Error("Network Request failed")
}) {
try {
val recentVersions = mockApi.getRecentAndroidVersions()
val versionFeaturesJobs = recentVersions.map {
androidVersion ->
async {
mockApi.getAndroidVersionFeatures(
androidVersion.apiLevel
)
}
}
val versionFeatures = versionFeaturesJobs.awaitAll()
uiState.value = UiState.Success(versionFeatures)
} catch (exception: Exception) {
uiState.value = UiState.Error("Network Request failed")
}
}
} |
Also fix typos in tests Fixes LukasLechnerDev#6, LukasLechnerDev#8
Hello @LukasLechnerDev it seems that the issue described here is still present in the current version of the course Using async directly rather than viewModelScope.async result in crash when the api response is in error |
Hi, Lukas!
I think there's a problem with the solution for
usecase4/VariableAmountOfNetworkRequestsViewModel#performNetworkRequestsConcurrently.
It crashes when I configure
MockApi
to return a500
on the call tohttp://localhost/android-version-features/28
.Reading this about exceptions, it seems like
try-catch
will not catch the exception becauseasync
is not the direct child of the scope -launch
is. Soasync
will propagate the exception up to its parent (launch
) andlaunch
will throw the exception.What do you think?
- Julian
The text was updated successfully, but these errors were encountered: