Flows, Channels and Reactive Streams

Harry · 23 Sep 2026 · 3 views
Log in to track your progress and mark lessons complete.

Cold Flows

fun orders() = flow {
    for (id in 1..5) {
        delay(200)
        emit("ORD-$id")
    }
}
runBlocking {
    orders().filter { it.endsWith("2") }
            .map { it.lowercase() }
            .collect { println(it) }
}

Flow Operators You Will Use

  • map/filter - transform like collections, lazily.
  • debounce - search-as-you-type without storms.
  • combine/zip - merge cart plus pricing streams.
  • catch/retry - errors as flow stages, not crashes.

Hot Flows: StateFlow and SharedFlow

val cartCount = MutableStateFlow(0)   // UI state holder
cartCount.value = 3                   // collectors update instantly

StateFlow powers modern Android UI state; SharedFlow powers one-shot events like snackbars.

Channels (One-to-One Pipes)

Use channels for worker pipelines and backpressure hand-offs; prefer Flow for observable data. Most app code never needs raw channels.

Flow pipeline plus StateFlow and SharedFlow

Key Points

  • Flow equals coroutines plus reactive streams.
  • StateFlow for state, SharedFlow for events.
  • Operators compose; collectors trigger execution.
Share this post:

Comments (0)

Please login or register to comment.

Create a free account to keep reading

You've enjoyed a free tutorial! Register (it's free) to unlock every tutorial, track your progress and save code.

or sign in with your account

Already have an account? Log in