Skip to content

Files

Latest commit

 

History

History
42 lines (27 loc) · 1 KB

GlobalCoroutineUsage.md

File metadata and controls

42 lines (27 loc) · 1 KB

Pattern: Global coroutine usage

Issue: -

Description

Report usages of GlobalScope.launch and GlobalScope.async. It is highly discouraged by the Kotlin documentation:

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.

Example of incorrect code:

fun foo() {
    GlobalScope.launch { delay(1_000L) }
}

Example of correct code:

val scope = CoroutineScope(Dispatchers.Default)

fun foo() {
    scope.launch { delay(1_000L) }
}

fun onDestroy() {
   scope.cancel()
}

Further Reading