Deep Kotlin tutorial covering flowOn, retry, catch, completion with examples, diagram, mistakes, and interview answers.

flowOn, retry, catch, Completion, and Cancellation

What is flowOn, retry, catch, Completion, and Cancellation?

flowOn, retry, catch, Completion, and Cancellation is an important Kotlin topic from 2.11 Flow Operators and Backpressure. In this lesson we focus only on the concepts that belong together: flowOn, retry, catch, completion, cancellation, dispatcher boundary. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

Why students should learn it

  • Understand flowOn, retry, catch, Completion, and Cancellation in simple language
  • Use flowOn in real Kotlin or Android code
  • Explain flowOn, retry, catch, Completion, and Cancellation in interviews with examples and trade-offs

How it works

flowOn, retry, catch, Completion, and Cancellation: flowOn, retry, catch, completion, cancellation, dispatcher boundary works by giving asynchronous work a clear owner and execution rule. In real Android code, first identify who owns the work, then choose the dispatcher, then decide how cancellation and errors should behave. This prevents background work from continuing after the screen or ViewModel no longer needs it.

  1. Identify what problem flowOn, retry, catch, Completion, and Cancellation: flowOn, retry, catch, completion, cancellation, dispatcher boundary solves.
  2. Write the smallest Kotlin snippet that demonstrates it.
  3. Add one Android or JVM use case where it matters.
  4. Compare it with a similar concept so the difference is clear.
  5. Finish by naming one common mistake and the safer alternative.
Interview tip: keep the answer simple first, then add example, trade-off, and mistake.

Types / Classification

  • flowOn: core concept in this lesson
  • retry: core concept in this lesson
  • catch: core concept in this lesson
  • completion: core concept in this lesson
  • cancellation: core concept in this lesson
  • dispatcher boundary: core concept in this lesson
  • Builder: creates the stream
  • Intermediate operator: transforms the stream

Important differences

Concept 1Concept 2How to remember it
ColdHotCold starts per collector; hot can emit without collectors.
StateEventState is current UI data; event is a one-time signal.
collectcollectLatestcollect finishes every value; collectLatest cancels old work for new values.
bufferconflatebuffer keeps every value queued for a slow collector; conflate drops intermediate values and keeps only the latest.

Diagram

flowchart TD
A["2.11 Flow Operators and Backpressure"] --> B["flowOn, retry, catch, Completion, and Cancellation"]
B --> C["Why it exists"]
C --> D["How it works in Kotlin"]
D --> E["Android or JVM example"]
E --> F["Mistakes and interview answer"]

Examples

Flow pipeline

val results = searchText
.debounce(300)
.distinctUntilChanged()
.flatMapLatest { query -> repository.search(query) }

Cold flow

fun observeCount(): Flow<Int> = flow {
emit(1)
emit(2)
}

When to use it

Use it when: Use Flow/StateFlow when a screen needs to react to changing data over time - a search results list updating as the user types, or a ViewModel exposing UI state to Compose or XML observers.

Avoid it when: Avoid Flow for a single one-shot value; a suspend function returning the value directly is simpler and easier to test.

Common mistakes

  • Collecting Flow without lifecycle awareness
  • Using StateFlow for one-time events
  • Forgetting that cold Flow restarts per collector
  • Choosing replay or buffer values without understanding delivery
  • Putting heavy work in the wrong dispatcher

Interview questions

Read each answer in four parts: simple answer, code example, trade-off, and common mistake.

1. What is flowOn, retry, catch, Completion, and Cancellation in 2.11 Flow Operators and Backpressure?

Answer: flowOn, retry, catch, Completion, and Cancellation is an important Kotlin topic from 2.11 Flow Operators and Backpressure. In this lesson we focus only on the concepts that belong together: flowOn, retry, catch, completion, cancellation, dispatcher boundary. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

val results = searchText
.debounce(300)
.distinctUntilChanged()
.flatMapLatest { query -> repository.search(query) }
  • Trade-off: Cold and Hot solve related but different problems: Cold starts per collector; hot can emit without collectors.
  • Common mistake: Collecting Flow without lifecycle awareness

2. How does flowOn, retry, catch, Completion, and Cancellation work in Kotlin or Android code?

Answer: flowOn, retry, catch, Completion, and Cancellation: flowOn, retry, catch, completion, cancellation, dispatcher boundary works by giving asynchronous work a clear owner and execution rule. In real Android code, first identify who owns the work, then choose the dispatcher, then decide how cancellation and errors should behave. This prevents background work from continuing after the screen or ViewModel no longer needs it.

fun observeCount(): Flow<Int> = flow {
emit(1)
emit(2)
}
  • Trade-off: Cold and Hot solve related but different problems: Cold starts per collector; hot can emit without collectors.
  • Common mistake: Using StateFlow for one-time events

3. What is one common mistake with flowOn, retry, catch, Completion, and Cancellation?

Answer: A common mistake is: Collecting Flow without lifecycle awareness Another is: Using StateFlow for one-time events

val results = searchText
.debounce(300)
.distinctUntilChanged()
.flatMapLatest { query -> repository.search(query) }
  • Trade-off: Cold and Hot solve related but different problems: Cold starts per collector; hot can emit without collectors.
  • Common mistake: Collecting Flow without lifecycle awareness

4. How would you explain flowOn, retry, catch, Completion, and Cancellation with a small example?

Answer: flowOn, retry, catch, Completion, and Cancellation is an important Kotlin topic from 2.11 Flow Operators and Backpressure. In this lesson we focus only on the concepts that belong together: flowOn, retry, catch, completion, cancellation, dispatcher boundary. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.

val results = searchText
.debounce(300)
.distinctUntilChanged()
.flatMapLatest { query -> repository.search(query) }
  • Trade-off: Cold and Hot solve related but different problems: Cold starts per collector; hot can emit without collectors.
  • Common mistake: Using StateFlow for one-time events

5. What is the trade-off of using flowOn, retry, catch, Completion, and Cancellation?

Answer: Cold and Hot solve related but different problems: Cold starts per collector; hot can emit without collectors.

fun observeCount(): Flow<Int> = flow {
emit(1)
emit(2)
}
  • Trade-off: Cold and Hot solve related but different problems: Cold starts per collector; hot can emit without collectors.
  • Common mistake: Collecting Flow without lifecycle awareness

Practice task

Write one Kotlin example for flowOn, retry, catch, Completion, and Cancellation. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.