Flow fundamentals
Flow Fundamentals Deep Dive¶
Overview¶
Flow is Kotlin's coroutine-based asynchronous stream API and a core part of modern Android data pipelines.
Core Concepts¶
Key properties of Flow:
- cold by default
- sequential by default
- cancellation-aware
- coroutine based
A cold stream starts producing values for each collector independently.
Internal Implementation¶
A flow builder defines a suspendable emission pipeline. Collection triggers execution.
That means a Flow is closer to a reusable recipe for producing values than a constantly running producer.
JVM / Compiler Behavior¶
Flow sits on top of coroutines and suspend machinery. Many operators are implemented as chains of suspendable transformations.
Code Examples¶
fun userFlow(): Flow<User> = flow {
emit(loadUser())
}
viewModelScope.launch {
userFlow().collect { user ->
render(user)
}
}
Common Interview Questions¶
- Q: What does βcoldβ mean in Flow? A: Start from delivery semantics: use StateFlow for durable state, SharedFlow or Channel for transient events, and lifecycle-aware collection to prevent duplicate work.
- Q: How is Flow different from LiveData or Rx streams? A: Start from delivery semantics: use StateFlow for durable state, SharedFlow or Channel for transient events, and lifecycle-aware collection to prevent duplicate work.
- Q: Why is Flow sequential by default? A: Start from delivery semantics: use StateFlow for durable state, SharedFlow or Channel for transient events, and lifecycle-aware collection to prevent duplicate work.
Production Considerations¶
Flow is powerful for:
- repository streams
- UI state pipelines
- database observation
- combining async sources
But careless operator chains and repeated collections can create inefficiency.
Performance Insights¶
Understand:
- collection triggers execution
- repeated collectors repeat cold work
- buffering/concurrency operators affect throughput and ordering
Senior-Level Insights¶
The strongest answers explain Flow not just as βreactive streams,β but as coroutine-native cold stream pipelines with explicit lifecycle and collection semantics.