Movatterモバイル変換


[0]ホーム

URL:


Kotlin Help

Debug Kotlin Flow using IntelliJ IDEA – tutorial

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

The tutorial assumes you have prior knowledge of thecoroutines andKotlin Flow concepts.

Create a Kotlin flow

Create a Kotlinflow 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 thekotlinx.coroutines library in a Gradle project, add the following dependency tobuild.gradle(.kts):

    dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2")}
    dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2'}

    For other build systems, see instructions in thekotlinx.coroutines README.

  3. Open theMain.kt file insrc/main/kotlin.

    Thesrc directory contains Kotlin source files and resources. TheMain.kt file contains sample code that will printHello World!.

  4. Create thesimple() function that returns a flow of three numbers:

    • Use thedelay() function to imitate CPU-consuming blocking code. It suspends the coroutine for 100 ms without blocking the thread.

    • Produce the values in thefor loop using theemit() function.

    import kotlinx.coroutines.*import kotlinx.coroutines.flow.*import kotlin.system.*fun simple(): Flow<Int> = flow { for (i in 1..3) { delay(100) emit(i) }}
  5. Change the code in themain() function:

    • Use therunBlocking() block to wrap a coroutine.

    • Collect the emitted values using thecollect() function.

    • Use thedelay() 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 theprintln() function.

    fun main() = runBlocking { simple() .collect { value -> delay(300) println(value) }}
  6. Build the code by clickingBuild Project.

    Build an application

Debug the coroutine

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

    Build a console application
  2. Run the code in debug mode by clickingDebug next to the run configuration at the top of the screen.

    Build a console application

    TheDebug tool window appears:

    • TheFrames tab contains the call stack.

    • TheVariables tab contains variables in the current context. It tells us that the flow is emitting the first value.

    • TheCoroutines tab contains information on running or suspended coroutines.

    Debug the coroutine
  3. Resume the debugger session by clickingResume Program in theDebug 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 usesuspend 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 cancause memory leaks.

Add a concurrently running coroutine

  1. Open theMain.kt file insrc/main/kotlin.

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

    • Add a call to thebuffer() function to run the emitter and collector concurrently.buffer() stores emitted values and runs the flow collector in a separate coroutine.

    fun main() = runBlocking<Unit> { simple() .buffer() .collect { value -> delay(300) println(value) }}
  3. Build the code by clickingBuild Project.

Debug a Kotlin flow with two coroutines

  1. Set a new breakpoint atprintln(value).

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

    Build a console application

    TheDebug tool window appears.

    In theCoroutines tab, you can see that there are two coroutines running concurrently. The flow collector and emitter run in separate coroutines because of thebuffer() function. Thebuffer() function buffers emitted values from the flow. The emitter coroutine has theRUNNING status, and the collector coroutine has theSUSPENDED status.

  3. Resume the debugger session by clickingResume Program in theDebug tool window.

    Debugging coroutines

    Now the collector coroutine has theRUNNING status, while the emitter coroutine has theSUSPENDED status.

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

06 September 2024
Debug coroutines using IntelliJ IDEA – tutorialSerialization

[8]ページ先頭

©2009-2026 Movatter.jp