https://kotlinlang.org logo
#android
Title
# android
d

dave08

01/13/2020, 10:47 AM
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

Ianmedeiros

01/13/2020, 2:04 PM
it’s ok
m

Michael Friend

01/13/2020, 3:51 PM
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

dave08

01/13/2020, 4:27 PM
Those are good points, thanks!
👍 1
2 Views