tutorial, coroutine_context, kotlin,

Kotlin - coroutine_context

Upendra Upendra Follow Jan 23, 2025 · 2 mins read
Kotlin - coroutine_context
Share this

Coroutine Scope vs Coroutine Context

CoroutineContext

Coroutines always execute in some context represented by a value of the CoroutineContext type, defined in the Kotlin standard library.

/**
 * Persistent context for the coroutine. 
 * It is an indexed set of [Element] instances.
 * An indexed set is a mix between a set and a map.
 * Every element in this set has a unique [Key].
 */
@SinceKotlin("1.3")
public interface CoroutineContext

CoroutineContext is just a Map that stores a set of Element objects that have a unique Key.

An Element is anything a coroutine might need to run correctly. The most common examples would be:

  • Job - a cancellable handle to a coroutine with a lifecycle;
  • CoroutineDispatcher, MainCoroutineDispatcher - dispatchers for a coroutine to run on;
  • CoroutineId, CoroutineName - elements mainly used to debug coroutines;
  • CoroutineExceptionHandler - an element that handles uncaught exceptions;
  • Continuation Interceptor - allows one to define how coroutines should continue.

CoroutineScope

A CoroutineScope is just a simple wrapper for a CoroutineContext. In a technical sense, it is nothing more than a CoroutineContext, and the only reason it exists and has a different name is to differentiate the intended purpose of the two.

public interface CoroutineScope {
    public val coroutineContext: CoroutineContext
}

CoroutineScope performs the following functions:

  • Provides an abstraction for manipulate contexts and jobs, help us to manage them;
  • CoroutineScope also keeps track of all its children’s scopes. e.g. when perform a launch within another scope, a child scope is created automatically;
  • It provides all the utilities to launch, async, and also cancel etc…;
  • Enforce structured concurrency.

Links

Coroutine context and dispatchers

Things every Kotlin Developer should know about Coroutines. Part 1: CoroutineContext

Things every Kotlin Developer should know about Coroutines. Part 2: CoroutineScope

Kotlin Coroutine Scope, Context, and Job made simple

Further reading

Coroutine Context and Scope

credit goes to @swayangjit
Join Newsletter
Get the latest news right in your inbox. We never spam!
Upendra
Written by Upendra Follow
Hi, I am Upendra, the author in Human and machine languages,I don't know to how 3 liner bio works so just Connect with me on social sites you will get to know me better.