使用 IntelliJ IDEA 调试 Kotlin Flow——教程

This tutorial demonstrates how to create Kotlin Flow and debug it using IntelliJ IDEA.

The tutorial assumes you have prior knowledge of the coroutines and Kotlin Flow concepts.

Create a Kotlin flow

Create a Kotlin flow with a slow emitter and a slow collector:

  1. Open a Kotlin project in IntelliJ IDEA. If you don’t have a project, create one.
  2. To use the kotlinx.coroutines library in a Gradle project, add the following dependency to build.gradle(.kts):

【Kotlin】

  1. dependencies {
  2. implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")
  3. }

【Groovy】

  1. dependencies {
  2. implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1'
  3. }

For other build systems, see instructions in the kotlinx.coroutines README.

  1. Open the Main.kt file in src/main/kotlin.

    The src directory contains Kotlin source files and resources. The Main.kt file contains sample code that will print Hello World!.

  2. Create the simple() function that returns a flow of three numbers:

    • Use the delay() function to imitate CPU-consuming blocking code. It suspends the coroutine for 100 ms without blocking the thread.
    • Produce the values in the for loop using the emit() function.
    1. import kotlinx.coroutines.*
    2. import kotlinx.coroutines.flow.*
    3. import kotlin.system.*
    4. fun simple(): Flow<Int> = flow {
    5. for (i in 1..3) {
    6. delay(100)
    7. emit(i)
    8. }
    9. }
  3. Change the code in the main() function:

    • Use the runBlocking() block to wrap a coroutine.
    • Collect the emitted values using the collect() function.
    • Use the delay() function to imitate CPU-consuming code. It suspends the coroutine for 300 ms without blocking the thread.
    • Print the collected value from the flow using the println() function.
    1. fun main() = runBlocking {
    2. simple()
    3. .collect { value ->
    4. delay(300)
    5. println(value)
    6. }
    7. }
  4. Build the code by clicking Build Project.

    Build an application

Debug the coroutine

  1. Set a breakpoint at the line where the emit() function is called:

    Build a console application

  2. Run the code in debug mode by clicking Debug next to the run configuration at the top of the screen.

    Build a console application

    The Debug tool window appears:

    • The Frames tab contains the call stack.
    • The Variables tab contains variables in the current context. It tells us that the flow is emitting the first value.
    • The Coroutines tab contains information on running or suspended coroutines.

      Debug the coroutine

  3. Resume the debugger session by clicking Resume Program in the Debug tool window. The program stops at the same breakpoint.

    Debug the coroutine

    Now the flow emits the second value.

    Debug the coroutine

Optimized-out variables

If you use suspend functions, in the debugger, you might see the “was optimized out” text next to a variable’s name:

Variable "a" was optimized out

This text means that the variable’s lifetime was decreased, and the variable doesn’t exist anymore. It is difficult to debug code with optimized variables because you don’t see their values. You can disable this behavior with the -Xdebug compiler option.

Never use this flag in production: -Xdebug can cause memory leaks.

使用 IntelliJ IDEA 调试 Kotlin Flow——教程 - 图8

Add a concurrently running coroutine

  1. Open the Main.kt file in src/main/kotlin.

  2. Enhance the code to run the emitter and collector concurrently:

    • Add a call to the buffer() function to run the emitter and collector concurrently. buffer() stores emitted values and runs the flow collector in a separate coroutine.
    1. fun main() = runBlocking<Unit> {
    2. simple()
    3. .buffer()
    4. .collect { value ->
    5. delay(300)
    6. println(value)
    7. }
    8. }
  3. Build the code by clicking Build Project.

Debug a Kotlin flow with two coroutines

  1. Set a new breakpoint at println(value).

  2. Run the code in debug mode by clicking Debug next to the run configuration at the top of the screen.

    Build a console application

    The Debug tool window appears.

    In the Coroutines tab, you can see that there are two coroutines running concurrently. The flow collector and emitter run in separate coroutines because of the buffer() function. The buffer() function buffers emitted values from the flow. The emitter coroutine has the RUNNING status, and the collector coroutine has the SUSPENDED status.

  3. Resume the debugger session by clicking Resume Program in the Debug tool window.

    Debugging coroutines

    Now the collector coroutine has the RUNNING status, while the emitter coroutine has the SUSPENDED status.

    You can dig deeper into each coroutine to debug your code.