KT.GLOBAL_COROUTINE_USAGE
Usage of GlobalScope instance
Global scope is used to launch top-level coroutines which are operating on the whole application lifetime and are not cancelled prematurely. Application code usually should use an application-defined CoroutineScope. Using async or launch on the instance of GlobalScope is highly discouraged.
Noncompliant Code
Copy
fun foo() {
GlobalScope.launch { delay(1_000L) }
}
Compliant Code
Copy
val scope = CoroutineScope(Dispatchers.Default)
fun foo() {
scope.launch { delay(1_000L) }
}
fun onDestroy() {
scope.cancel()
}