Kotlin
flowOn
Learn context switching with flowOn.
flowOn: Changing context
flowOn(dispatcher) sets the coroutine context for the upstream of the flow. Upstream means the builder and intermediate operators before flowOn. collect keeps running in the caller's context.
fun numbers(): Flow<Int> = flow {
for (i in 1..3) emit(i)
}
fun main() = runBlocking {
numbers()
.flowOn(Dispatchers.IO)
.collect { println(it) }
}
Dispatchers.IO is recommended for file/network I/O, Dispatchers.Default for CPU work. The order of values is preserved; only the thread that runs emit changes.
The upstream concept
The position of flowOn matters: it only affects the part before it. So the builder (flow { }) and intermediate operators (map, filter) run in the specified context. collect stays in the caller's context.
numbers()
.map { it * 2 } // in flowOn context
.flowOn(Dispatchers.Default)
.collect { println(it) } // in runBlocking context
The order and content of values are preserved; you only move where the computation happens. This is useful to keep slow work such as network calls or file reads off the UI thread.
Exercise Set the context with flowOn
Define fun numbers(): Flow<Int> = flow { ... }. In the body use a for (i in 1..3) loop with emit(i). Mark main with runBlocking. Inside it call numbers().flowOn(Dispatchers.IO).collect { println(it) }. The output prints each value on its own line.
Show example solution
fun numbers(): Flow<Int> = flow {
for (i in 1..3) {
emit(i)
}
}
fun main() = runBlocking {
numbers().flowOn(Dispatchers.IO).collect { println(it) }
}
Test yourself
-
Change the context
numbers().____(Dispatchers.IO).collect { println(it) }
Show answer
numbers().flowOn(Dispatchers.IO).collect { println(it) }
-
What does flow.flowOn(Dispatchers.IO) change?
- The value type of the flow
- The context of the collect call
- The context the upstream of the flow runs on
- The cold nature of the flow
Show answer
The context the upstream of the flow runs on — flowOn sets the context for the upstream (builder and intermediate operators).
-
Match context and dispatcher
- flowOn
- Sets context
- Dispatchers.IO
- I/O work
- Dispatchers.Default
- CPU work
- upstream
- Above the call
Next lesson: Summary - Flow basics