Florian
10/29/2020, 10:54 AM@ApplicationScope
@Provides
@Singleton
fun provideApplicationScope() = CoroutineScope(SupervisorJob())
Should I also inject the dispatcher that is used with this scope, and why?gildor
10/29/2020, 12:20 PMFlorian
10/29/2020, 3:25 PMFlorian
10/29/2020, 3:30 PMtravis
10/29/2020, 10:31 PMCoroutineScope
written, it would exhibit different behavior than GlobalScope
since you're using SupervisorJob
.
You can additionally have an exception handler (and other customizations).
Without those customizations, it would essentially be the same as GlobalScope
. I think that comes down to preference at that point.
I personally think GlobalScope
is a good choice if you want that specific behavior (i.e. on Android crashing app if an uncaught exception is thrown) as the GlobalScope
in your code makes it explicit the behavior that you'll have with it.
Depending on your architecture though, it might make more sense to use a custom scope that you inject, to make swapping it out easier in unit tests.
It probably comes down to how you want your structured concurrency to be organized (what scope you want to be the parent for Coroutines you'll be launching).gildor
10/30/2020, 1:29 AMFlorian
10/30/2020, 9:41 AM