Introduction
The Kotlin Flow combine operator waits for each input flow to emit at least one value before producing its first combined output. If any of the combined flows has not emitted yet (e.g., a cold flow that has not been collected, a SharedFlow with zero replay, or a StateFlow that has not been assigned), combine silently waits and emits nothing. This is a common source of confusion when combining flows with different emission patterns, such as mixing hot and cold flows, or when one flow's initial emission is delayed.
Symptoms
combineblock never executes despite individual flows emitting- UI stays blank when combining user state and settings state
zipcompletes butcombinedoes not emit- One flow emits many values but combined output is empty
- Flow chain appears correct but downstream collector receives nothing
Common Causes
- One of the flows in
combinehas not emitted its first value - Using
SharedFlowwithreplay = 0— new collectors get no replayed value - Cold flow inside
combineis not triggered until collected - Flow collection started after upstream already emitted
- Using
zip(which pairs by index) instead ofcombine(which pairs latest)
Step-by-Step Fix
- 1.Ensure all flows have initial values for combine:
- 2.```kotlin
- 3.// WRONG - one flow has no emissions, combine waits forever
- 4.val userFlow = repository.getUserFlow() // Emits after network call (delayed)
- 5.val settingsFlow = MutableStateFlow(Settings()) // Has initial value
// combine waits for BOTH to emit — userFlow is delayed combine(userFlow, settingsFlow) { user, settings -> UiState(user, settings) // Never called until userFlow emits }
// CORRECT - use StateFlow for both with initial values val userFlow = MutableStateFlow<User?>(null) // Starts with null val settingsFlow = MutableStateFlow(Settings()) // Has initial value
combine(userFlow, settingsFlow) { user, settings -> UiState(user, settings) // Called immediately with null user } .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), UiState())
// If userFlow comes from repository, wrap it val userState: StateFlow<User?> = repository.getUserFlow() .onStart { emit(null) } // Emit initial null before real data .stateIn(viewModelScope, SharingStarted.Lazily, null) ```
- 1.Use SharedFlow with replay for combine compatibility:
- 2.```kotlin
- 3.// WRONG - SharedFlow with replay = 0, combine waits forever
- 4.val eventsFlow = MutableSharedFlow<UiEvent>(replay = 0)
- 5.val stateFlow = MutableStateFlow(UiState())
combine(eventsFlow, stateFlow) { event, state -> // Never called - eventsFlow has replay = 0, so combine waits for first emission state.copy(lastEvent = event) }
// CORRECT - use replay = 1 so combine gets the latest value val eventsFlow = MutableSharedFlow<UiEvent>( replay = 1, extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST )
// OR convert SharedFlow to StateFlow for combine val latestEvent = eventsFlow .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), null)
combine(latestEvent, stateFlow) { event, state -> state.copy(lastEvent = event) }
// Alternative: use map and combine with stateFlow only stateFlow .map { state -> state.copy(lastEvent = currentEvent) } ```
- 1.Debug flow emissions to find the missing flow:
- 2.```kotlin
- 3.// Add logging to each flow to find which one is not emitting
- 4.val flowA = repository.getUsers()
- 5..onEach { Log.d("Flow", "flowA emitted: $it") }
- 6..onStart { Log.d("Flow", "flowA started") }
- 7..onCompletion { cause -> Log.d("Flow", "flowA completed: $cause") }
val flowB = repository.getSettings() .onEach { Log.d("Flow", "flowB emitted: $it") } .onStart { Log.d("Flow", "flowB started") } .onCompletion { cause -> Log.d("Flow", "flowB completed: $cause") }
combine(flowA, flowB) { a, b -> Log.d("Flow", "combine: a=$a, b=$b") CombinedState(a, b) } .onEach { Log.d("Flow", "combined result: $it") } .collect { /* ... */ }
// If you see "flowA started" but no "flowA emitted", the upstream is blocked // If you see "flowA emitted" but combine does not fire, check flowB ```
- 1.Use zip vs combine correctly:
- 2.```kotlin
- 3.// combine - emits whenever ANY flow emits (after all have emitted once)
- 4.// Uses the LATEST value from each flow
- 5.combine(flowA, flowB) { a, b -> Pair(a, b) }
- 6.// flowA: 1 --- 2 --- 3
- 7.// flowB: A ------- B
- 8.// result: (1,A) (2,A) (3,A) (3,B)
// zip - emits when BOTH flows have emitted at the same index // Pairs by position, not by time zip(flowA, flowB) { a, b -> Pair(a, b) } // flowA: 1 --- 2 --- 3 --- 4 // flowB: A ------- B // result: (1,A) (2,B) // Stops here - flowB has no more values
// Use combine for reactive UI state (latest of everything) // Use zip for pairing request-response or sequential operations ```
Prevention
- Use
StateFlowinstead ofSharedFlowwhen combining — StateFlow always has a value - Add
onStart { emit(initialValue) }to flows that may not emit immediately - Log flow emissions during development to debug silent combine failures
- Use
stateInto convert cold flows to hot StateFlow with initial values - Test combined flows with Turbine to verify emission timing
- Remember:
combineneeds ALL flows to emit at least once before first output