rememberSuspending

fun <T> rememberSuspending(coroutineContext: CoroutineContext = Dispatchers.Unconfined, useLastWhileLoading: Boolean = false, action: suspend CalculationContext.() -> T): Reactive<T>(source)

This is a suspending version of remember. Creates a reactive value that automatically updates when its dependencies change.

The action block is executed in a reactive context, and its result is cached and shared among listeners. The calculation runs in a coroutine, and will re-run whenever any reactive value it depends on changes.

Note:

  • remember is lazy: if it has no listeners, it will not calculate a value.

  • Listeners are only notified if the calculated value changes (i.e., if the new value is different from the previous value).

Return

A Reactive value that updates automatically.

Example:

val a = Signal(0)
val b = Signal(1)
val sum: Reactive<Int> = rememberSuspending { a() + b() }

reactiveSuspending {
println("sum: ${sum()}") // prints "sum: 1"
}

a.value = 1 // prints "sum: 2"
b.value = 2 // prints "sum: 3"

Parameters

coroutineContext

The coroutine context for running the calculation (default: Dispatchers.Unconfined).

useLastWhileLoading

If true, uses the last known value while recalculating.

action

The block to compute the value reactively.

See also