Getting AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1 with no other stack trace

Viewed 3739

When using Retrofit and Coroutines to fetch data from an API I sometimes get an app crash with no stacktrace in Logcat except this: AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1

3 Answers

This is usually caused by Retrofit throwing a common exception like UnknownHostException (if there's no Internet) and coroutines swallowing the exception if you didn't specify a CoroutineExceptionHandler.

So add a coroutine exception handler in your launch code, something like this:

val coroutineExceptionHandler = CoroutineExceptionHandler{_, throwable ->
    throwable.printStackTrace()
}

fun getFromApi() {
    viewModelScope.launch(Dispatchers.IO + coroutineExceptionHandler) {
        retrofitService.getStuffFromInternet()
    }
}

Note this just logs the error to Logcat so you can see the missing stacktrace. You still need to figure out what's causing it.

I have encountered this type of error many times and what worked for me was to uninstall the app from the emulator and then installing it again.

Default Dispatcher Worker - 1 or Default Dispatcher Worker - 3, I don't know the reason for these errors but I think it is some issue with the emulator device you are using or maybe if you have import some other project and run it on that emulator device, than later on you might see this error.

What you can do to solve this issue : Just create a new emulator device or use a physical device.

Related