Recall & Review
beginner
What is Flow context preservation in Kotlin?
It means that when a Flow emits values, it keeps the coroutine context (like dispatcher) consistent across its operations, ensuring predictable execution.
Click to reveal answer
beginner
Why is context preservation important in Kotlin Flows?
Because it ensures that all operators in the Flow run on the expected thread or dispatcher, avoiding unexpected thread switches and bugs.
Click to reveal answer
intermediate
How does
flowOn affect Flow context preservation?flowOn changes the coroutine context upstream of where it is applied, allowing you to control where emissions happen without affecting downstream collectors.Click to reveal answer
intermediate
What happens if you use
withContext inside a Flow builder?Using
withContext inside a Flow builder can break context preservation because it switches context inside the emission block, which is discouraged.Click to reveal answer
intermediate
How can you ensure a Flow preserves its context when collecting?
Collect the Flow in the desired coroutine context or dispatcher, and use
flowOn to control upstream context without switching downstream context.Click to reveal answer
What does
flowOn do in Kotlin Flows?✗ Incorrect
flowOn changes the coroutine context for the upstream flow emissions only.Which operator can break Flow context preservation if used inside a Flow builder?
✗ Incorrect
withContext switches coroutine context inside the builder and can break context preservation.Why should you avoid using
withContext inside a Flow builder?✗ Incorrect
Using
withContext inside a Flow builder breaks context preservation and can cause bugs.How do you keep the downstream collector on the main thread while running emissions on IO dispatcher?
✗ Incorrect
flowOn(Dispatchers.IO) runs upstream emissions on IO but keeps downstream collection context unchanged.What ensures that a Flow runs on the expected thread or dispatcher?
✗ Incorrect
Context preservation keeps the Flow running on the expected coroutine context.
Explain what Flow context preservation means and why it matters in Kotlin coroutines.
Think about how Flow operators run on threads.
You got /3 concepts.
Describe how
flowOn helps control the execution context of a Flow.Consider where emissions happen vs where collection happens.
You got /3 concepts.