Coder Social home page Coder Social logo

usecase4/VariableAmountOfNetworkRequestsViewModel#performNetworkRequestsConcurrently crashes on exception about kotlin-coroutines-and-flow-usecases-on-android HOT 3 OPEN

lukaslechnerdev avatar lukaslechnerdev commented on May 16, 2024
usecase4/VariableAmountOfNetworkRequestsViewModel#performNetworkRequestsConcurrently crashes on exception

from kotlin-coroutines-and-flow-usecases-on-android.

Comments (3)

lgtout avatar lgtout commented on May 16, 2024

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.

lgtout avatar lgtout commented on May 16, 2024

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.

kaal-dam avatar kaal-dam commented on May 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.