https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

maxmello

09/03/2021, 12:59 PM
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

Lefko

09/03/2021, 2:38 PM
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

maxmello

09/06/2021, 12:18 PM
Great, thank you for linking this thread 🙂
4 Views