liveBook · Manning

Chapter 16: Flows

What you will learn in chapter 16 of Kotlin in Action, 2E

Introduces flows as a coroutine-based way to work with sequential values over time.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces flows as a coroutine-based way to work with sequential values over time.

Key terms: flows, coroutines, sequential values

16.1 Flows model sequential streams of values

This section explains why flows are useful when a computation produces values over time instead of returning them all at once.

Key terms: suspending function, flows, progressive loading, streams of events, subscription-style APIs

16.1.1 Flows allow you to work with elements as they are emitted

This section shows how a flow lets code process values as they are emitted instead of waiting for an entire batch.

Key terms: flow builder function, emit, collect, flows

16.1.2 Different types of flows in Kotlin

This subsection distinguishes cold and hot flows by when they produce values and whether production depends on a collector.

Key terms: cold flows, hot flows, collector, broadcast

16.2 Cold flows

This introduction previews a closer look at creating, running, cancelling, and using concurrency in cold flows.

16.2.1 Creating a cold flow with the flow builder function

This section shows how to create a cold flow with the flow builder and explains why its work waits until collection.

Key terms: flow builder, emit, collector, suspend, Flow<T>, cold, collected, terminal operator, cold flow, non-suspending

16.2.2 Cold flows don’t do any work until collected

This section explains that collecting a cold flow runs its code, how collection and emission proceed together, and what repeated or unending collection implies.

Key terms: collect, Flow, collector, emission, suspending function, flow builder, delay, cold flow, side effects, cancel

16.2.3 Cancelling the collection of a flow

This subsection explains how cancelling a collector’s coroutine stops flow collection.

Key terms: flow collector, coroutine, cancellation point, emit

16.2.4 Cold flows under the hood

Explains how a cold flow uses a builder lambda and a collector lambda to emit values during collection.

Key terms: cold flow, flow builder function, Flow, FlowCollector, collect, emit

16.2.5 Concurrent flows with channel flows

This section explains why regular cold flows emit sequentially, how channel flows allow concurrent work, and when the extra complexity is worthwhile.

Key terms: flow builder function, emit, flow invariant, FlowCollector, channelFlow, channel flow, send, collector, coroutine scope, regular cold flows

16.3 Hot flows

Introduces hot flows as streams that share emissions among subscribers and identifies Kotlin’s two hot-flow types.

Key terms: hot flows, cold flows, collectors, subscribers, emissions, Shared flows, State flows, broadcasting values, communicating state

16.3.1 Shared flows broadcast values to subscribers

This section explains how shared flows broadcast values, how subscribers receive and replay them, and how to share the work of a cold flow.

Key terms: SharedFlow, subscriber, collector, broadcast, MutableSharedFlow, _messageFlow, messageFlow, beginBroadcasting, collect, replay

16.3.2 Keeping track of state in your system: State flow

This section explains how state flows hold a changing value, support safe updates, emit only changed values, and can be created from cold flows.

Key terms: state flow, MutableStateFlow, StateFlow, value, collect, update, atomic updates, equality-based conflation, collectors, stateIn

16.3.3 Comparing state flows and shared flows

Compares shared flows and state flows, then shows how storing message history as state can serve subscribers who arrive late.

Key terms: hot flows, shared flows, state flows, equality-based conflation, subscriber, shared flow, state flow, message history, replay cache

16.3.4 Hot, cold, shared, state: When to use which flow

This section gives a rule of thumb for choosing cold versus hot flows and notes how Kotlin flows can interoperate with other reactive-stream implementations.

Key terms: cold flows, state flows, shared flows, collect, built-in conversion functions, Kotlin flows, reactive streams

16.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 15: Structured concurrency Chapter 17: Flow operators →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.