I'm having a hard time understanding the usages and documentation for GlobalScope. The docs states:
A global CoroutineScope not bound to any job.
Global scope is used to launch top-level coroutines which are operating on the whole application lifetime and are not cancelled prematurely. Another use of the global scope is operators running in Dispatchers.Unconfined, which don’t have any job associated with them.
Application code usually should use an application-defined CoroutineScope. Using async or launch on the instance of GlobalScope is highly discouraged.
What does it mean when GlobalScope is not bound to any job? Because I can do
val job = GlobalScope.launch { // some work } job.cancel()
It says they are not canceled prematurely. What does this mean? As you can see above I can cancel it.
Lastly it says, it operates on the whole application lifetime. So the scope stays alive until the application dies. How does this compare with
CoroutineScope
? When I exit an AndroidActivity
in the middle of a runningCoroutineScope
, it'll still be alive and runs until completion. Does it just mean theCoroutineScope
will get cleaned up w/ garbage collection after it completes andGlobalScope
wont?
link to docs: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-global-scope/