Skip to content
Commitwise
Menu

Structured concurrency

Organizing concurrent work so every task has a parent that waits for it, cancels it and receives its errors.

Definition

With structured concurrency, background work is started inside a scope: a Kotlin CoroutineScope, a Swift task group or async let. The scope can't finish until its children finish, cancelling the scope cancels the children, and a child's failure is reported to the parent. Work therefore can't leak past the screen or request that started it. Unstructured tasks such as GlobalScope.launch or Task.detached opt out of these guarantees.

In practice

viewModelScope cancels every coroutine it started when the ViewModel is cleared, including in-flight network requests.

Learn it in context

Lessons that use this term.

Related terms

Esc

Type to search the whole site.