Hey, this is an Android + Coroutine question, but ...
# coroutines
m
Hey, this is an Android + Coroutine question, but it would technically apply to all programs that have some global state, so I decided to ask it here: In my classes that are effectively singletons, which are the classes of which I create one instance inside my
Application
class and always reference them via the
Application
instance, does it make any difference if I use
GlobalScope.launch
to launch a coroutine, or let my
Application
extend
CoroutineScope
and pass in the
Application
instance to the class to do
application.launch
instead? I ask because of the (new?) warnings about
DelicateCoroutinesApi
when using GlobalScope. When I use the second approach, the warning goes away, but I would assume they are functionally the same as both the Application’s coroutineScope and GlobalScope live the whole time the app is running? (Note this is mostly about doing things once asynchronously on start of the application)
l
There are other reasons to avoid global scope (outlined here). TLDR - Global scope does not work with structured concurrency out of the box. You need to manually manage coroutines launched in GlobalScope.
đź‘Ť 1
m
Great, thank you for linking this thread 🙂