0
0
Kotlinprogramming~10 mins

FlowOn for changing dispatcher in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - FlowOn for changing dispatcher
Start Flow
Emit values on IO Dispatcher
Apply flowOn(Dispatchers.IO)
Switch context to IO Dispatcher
Collect values on Default Dispatcher
End Flow
flowOn switches the context to the IO dispatcher for upstream operations like emitting values, while collection happens on the Default dispatcher.
Execution Sample
Kotlin
flow {
    emit(1)
    emit(2)
}.flowOn(Dispatchers.IO).collect { value ->
    println("Collected $value on ${Thread.currentThread().name}")
}
This code emits two values in a flow on the IO dispatcher via flowOn, then collects and prints each value with the current thread name on the Default dispatcher.
Execution Table
StepActionDispatcher/ThreadValue Emitted/CollectedOutput
1Start flow builderDefault dispatcher--
2Emit value 1IO dispatcher1-
3Switch context with flowOnIO dispatcher--
4Collect value 1Default dispatcher1Collected 1 on Default-thread
5Emit value 2IO dispatcher2-
6Collect value 2Default dispatcher2Collected 2 on Default-thread
7Flow completesDefault dispatcher--
💡 Flow completes after all values are emitted and collected with context switched by flowOn.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
value-1122
dispatcherDefaultIODefaultDefaultDefault
Key Moments - 2 Insights
Why does the collection happen on the default dispatcher and not the IO dispatcher?
Because flowOn changes the upstream context to IO dispatcher, so collection runs on the original Default dispatcher as shown in steps 4 and 6 of the execution_table.
Does flowOn change the dispatcher for the emit calls?
Yes, emit calls happen on the IO dispatcher as seen in steps 2 and 5; flowOn changes the upstream dispatcher for emit calls, while downstream operations like collect run on original.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, on which dispatcher is the value 1 collected?
AMain dispatcher
BDefault dispatcher
CIO dispatcher
DUnconfined dispatcher
💡 Hint
Check step 4 in the execution_table where collection happens on Default dispatcher.
At which step does the flow switch context to the IO dispatcher?
AStep 3
BStep 2
CStep 5
DStep 6
💡 Hint
Look at the 'Action' column in execution_table for flowOn context switch.
If flowOn was removed, where would collection happen?
AOn Main dispatcher
BOn IO dispatcher
COn Default dispatcher
DOn Unconfined dispatcher
💡 Hint
Without flowOn, collection stays on the original dispatcher as shown by variable 'dispatcher' in variable_tracker.
Concept Snapshot
flowOn changes the dispatcher for upstream flow operations.
Emit runs on dispatcher set by flowOn.
Collect runs on original dispatcher.
Useful to switch context for heavy or IO tasks.
Syntax: flow.flowOn(Dispatchers.IO).collect { ... }
Full Transcript
This example shows how Kotlin Flow emits values on the IO dispatcher. When flowOn is applied with Dispatchers.IO, the upstream operations like emit run on the IO dispatcher. The execution table traces each step: starting the flow, emitting values on the IO dispatcher, switching context with flowOn, and collecting values on the Default dispatcher. Variables track the current value and dispatcher context. Key moments clarify that flowOn affects only upstream operations like emit calls, not downstream collect. The visual quiz tests understanding of dispatcher switching and flowOn behavior. The concept snapshot summarizes that flowOn changes the dispatcher for upstream flow operations, allowing context switching for tasks like IO.