Mateu
06/01/2023, 11:38 AMandylamax
06/01/2023, 11:44 AMMainScope()
usually is the one hooked to the Main thread. While your use case might wan't you to crash your UI. Most popular use cases are to handle those errors gracefully and display them on (you guessed it), a UI.
So, thats why MainScope()
uses a SupervisorJob()
uli
06/01/2023, 4:12 PMandylamax
06/01/2023, 4:43 PMuli
06/01/2023, 4:47 PMUI can't do any more work, hence it crashes
???
andylamax
06/01/2023, 5:08 PMuli
06/01/2023, 5:11 PMandylamax
06/01/2023, 5:24 PMCoroutineExceptionHandler
crashes (Not cancelled, Crashes), it cancels the Job. Any subsequent launches to this Job will throw (which if not handled properly, will crash the system), This behaviour is not the same for a SuoervisorJob.
if a similar coroutine is launched and crashes, subsequent launches from this SupervisorJob won't throw (cause it was never cancelled).
Now, to the MainScope()
Most UI frameworks (if not all), launch their UI related tasks from the main scope. If the MainScope was backed by a Job
any further tasks launched by the UI (i.e event scheduling and painting, and even Layouting) would be throwing a CoroutineCancelledException (Do not quote me on that).
But because the MainScope is backed by a SupervisorJob
(and not a regular job) it supports subsequent launches even where there are child coroutines failing or they are being cancelleduli
06/02/2023, 10:01 AMIf the exception is not handled and theThe point is, Using a SupervisorJob does not prevent your App from crashing if you don’t catch your exceptions. Installing adoesn’t have aCoroutineContext
(as we’ll see later), it will reach the default thread’sCoroutineExceptionHandler
. In the JVM, the exception will be logged to console; and in Android, it will make your app crash regardless of the Dispatcher this happens on.ExceptionHandler
CoroutineExceptionHandler
could be a solution though.andylamax
06/02/2023, 2:37 PMCoroutineExceptionHandler
as I mentioned above, the whole context is about coroutines which have't been supplied a CoroutineExceptionHandler
There are plenty of uses cases where one would consciously opt not to pass a CoroutineExceptionHandler
but thats not the topic.
Putting all that into context, now point out to me exactly which statement is not exactly true. I'd like to relearn if relearning is dueandylamax
06/02/2023, 2:44 PM