# Kotlin Flow Debugging: Trace combine and flatMapLatest with onEach

DevFeed: [Kotlin Flow Debugging: Trace combine and flatMapLatest with onEach](<https://devfeed.tech/articles/kotlin-flow-debugging-trace-combine-and-flatmaplatest-with-oneach-105767.md>)

Original publisher: [Read original article](<https://proandroiddev.com/kotlin-flow-debugging-trace-combine-and-flatmaplatest-with-oneach-863b337f945f?source=rss----c72404660798---4>)

Author: chanzmao

Published: 2026-10-29T01:59:28Z

Content type: tutorial

Language: en

Sources: [ProAndroidDev](<https://devfeed.tech/sources/proandroiddev-medium.md>)

Topics: [coroutines-flow](<https://devfeed.tech/topics/coroutines-flow.md>), [Functional programming](<https://devfeed.tech/topics/functional-programming.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [debugging](<https://devfeed.tech/tags/debugging.md>), [flow](<https://devfeed.tech/tags/flow.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [logging](<https://devfeed.tech/tags/logging.md>)

## AI overview

Kotlin Flow's `onEach` operator lets you log each emitted value without changing it, but its action runs only when the Flow is collected. Place it on inputs to `combine` and around pipeline operators to see emissions, recomputations, and where values are filtered or transformed. With `flatMapLatest`, log the outer queries and inner results separately; use `onCompletion` to inspect a prior inner Flow's completion cause when a new query cancels it. These logs help reveal behavior, but close or concurrent emissions do not guarantee a one-to-one causal order with `combine` calls. For exact correlation, the excerpt recommends explicit event identifiers or separately modeled trigger events. Emission logs also do not show internal work such as when a network request starts or the server responds; log at the repository or network boundary for that.

## Source excerpt

Inspect emissions, find unexpected recomputations, and debug Flow pipelines with minimal code. Image created by AIWhere Did This Emission Come From? Kotlin Flow makes it easy to build declarative data pipelines. We can combine multiple streams with combine, switch between upstream flows with flatMapLatest, and transform values with operators such as map and filter. But as a pipeline grows, debugging becomes surprisingly difficult. Imagine a ViewModel that combines repository data, a search query, and a UI preference: val uiState = combine( repository.items, searchQuery, showAds, ) { items, query, showAds -> createUiState(items, query, showAds) } When createUiState() runs, which input triggered it? Did the repository emit new items? Did the user change the search query? Did the ad preference change? Or did several inputs emit values in quick succession? The combine lambda receives the latest value from every upstream Flow, but it doesn't tell us which Flow emitted most recently. We could introduce wrapper classes, restructure the pipeline, or add logging throughout the codebase. However, for everyday debugging, there is a much simpler solution: onEach. It lets us inspect values as they pass through a Flow without transforming them or changing the pipeline's data model. Let's see how to use it effectively. Why onEach Is the Easiest Way to Inspect a Flow The onEach operator performs an action for every emitted value before passing that value downstream. flow .onEach { value -> println("Emitted: $value") } .map { value -> transform(value) } The important point is that onEach doesn't transform the emitted value. This makes onEach ideal for temporary diagnostic logging. We can insert it into an existing pipeline, inspect what's happening, and remove it when we're done. There is one important detail: Flow is generally lazy. Adding onEach doesn't start collection by itself. Its action runs only when the Flow is collected and an upstream value is emitted. Identify Which Flow