Jonathan
09/17/2018, 2:34 PMunsafeMethod
fails it will make the parent job fail, which basically mean a crash of the UI componenent (any working coroutine will be cancelled and further action would no longer work)
Of course I could encapsulate the call in try-catch
. But it is a bit boilerplate and easy to forget. (here it is obvious, but what about unexpected exceptions?)
How could I launch a child coroutine in a way that it does'nt propagate the failure to the parent?Pavel.AZ
09/17/2018, 2:41 PMJonathan
09/17/2018, 2:43 PMPavel.AZ
09/17/2018, 2:46 PMJonathan
09/17/2018, 2:46 PMUncaughtExceptionHandler
.Pavel.AZ
09/17/2018, 2:47 PMJonathan
09/17/2018, 2:47 PMor try/catch every block in development stage if you unsureThat's what I want to avoid. And what about production? It is even more true for production code. I don't want the UI to crash in production either, do I?
Pavel.AZ
09/17/2018, 2:53 PMJonathan
09/17/2018, 2:54 PMtry-catch
and exception are managed.try-catch
blocks and unexpected are logged and displayed to the user using an UncaughtExceptionHandler
.
But still I'd like that the UI remains usable, even after unexpected exceptions.abstract class ExceptionSafeCoroutineScope(private val dispatcher: CoroutineDispatcher) : CoroutineScope {
private var job = Job()
private val exceptionHandler = CoroutineExceptionHandler { _, exception ->
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), exception)
job = Job()
}
final override val coroutineContext: CoroutineContext
get() = job + dispatcher + exceptionHandler
}
But I wonder what is the recommended way of doing it.kevinherron
09/17/2018, 3:52 PMGlobalScope.launch { ... }
Jonathan
09/17/2018, 4:21 PMelizarov
09/17/2018, 5:02 PMJonathan
09/18/2018, 6:32 AM