Is it ok to use `GlobalScope` for coroutines that ...
# android
d
Is it ok to use
GlobalScope
for coroutines that need to be tied to the
Application
? There doesn't seem to be a
lifecycleScope
for it... or is it better to create a
CoroutineScope
and cancel it somewhere in the
Application
?
i
it’s ok
m
I would refer to https://medium.com/@elizarov/the-reason-to-avoid-globalscope-835337445abc by Roman to see the potential gotchas. Also consider how exceptions are handled in
GlobalScope
. See https://kotlinlang.org/docs/reference/coroutines/exception-handling.html#coroutineexceptionhandler for how to handle exceptions yourself when using
GlobalScope
. Also notice that
viewModelScope
and
lifeCycleScope
implementations both use a
SupervisorJob
to handle exceptions in children, which you wouldn’t get in
GlobalScope
d
Those are good points, thanks!
👍 1