Comments (3)
Just did some more investigating. I see that the exception gets caught by the try-catch
.
It is kotlinx.coroutines.JobCancellationException: Parent job is Cancelling; job=StandaloneCoroutine{Cancelling}@926b1ac
Then the app crashes.
from kotlin-coroutines-and-flow-usecases-on-android.
I read your post about coroutine exceptions, which confirmed that the try-catch
inside launch
will catch calls to getAndroidVersions()
, but will not catch calls to getAndroidVersionFeatures()
because those take place within a non-top-level async
.
Any exception thrown within the async
is propagated to the root launch
. And the only way to catch an exception propagated in this way is by installing a CoroutineExceptionHandler
.
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")
}
}
}
from kotlin-coroutines-and-flow-usecases-on-android.
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
from kotlin-coroutines-and-flow-usecases-on-android.
Related Issues (10)
- Usecase for StateFlow and SharedFlow HOT 1
- For case 13(Exception handling),the last sub-case is missing. HOT 1
- Main or IO thread HOT 4
- Perform several network requests concurrently HOT 1
- Add info about Workmanager gotcha HOT 2
- Propagating state HOT 1
- Typo in name of test function HOT 1
- Corounties
- This can be done by synchronously with the help of withContext?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlin-coroutines-and-flow-usecases-on-android.