Skip to content

update: adding kotlinx.dependencies #3394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions docs/topics/debug-coroutines-with-idea.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,39 @@ This tutorial demonstrates how to create Kotlin coroutines and debug them using

The tutorial assumes you have prior knowledge of the [coroutines](coroutines-guide.md) concept.

> Debugging works for `kotlinx-coroutines-core` version 1.3.8 or later.
>
{type="note"}

## Create coroutines

1. Open a Kotlin project in IntelliJ IDEA. If you don't have a project, [create one](jvm-get-started.md#create-an-application).
1. Open a Kotlin project in IntelliJ IDEA. If you don't have a project, [create one](jvm-get-started.md#create-a-project).
2. To use the `kotlinx.coroutines` library in a Gradle project, add the following dependency to `build.gradle(.kts)`:

<tabs group="build-script">
<tab title="Kotlin" group-key="kotlin">

```kotlin
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:%coroutinesVersion%")
}
```

</tab>
<tab title="Groovy" group-key="groovy">

```groovy
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:%coroutinesVersion%'
}
```

</tab>
</tabs>

2. Open the `main.kt` file in `src/main/kotlin`.
For other build systems, see instructions in the [`kotlinx.coroutines` README](https://github.com/Kotlin/kotlinx.coroutines#using-in-your-projects).

3. 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!`.
The `src` directory contains Kotlin source files and resources. The `Main.kt` file contains sample code that will print `Hello World!`.

3. Change code in the `main()` function:
4. Change code in the `main()` function:

* Use the [`runBlocking()`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html) block to wrap a coroutine.
* Use the [`async()`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/async.html) function to create coroutines that compute deferred values `a` and `b`.
Expand All @@ -39,7 +59,7 @@ The tutorial assumes you have prior knowledge of the [coroutines](coroutines-gui
}
```

4. Build the code by clicking **Build Project**.
5. Build the code by clicking **Build Project**.

![Build an application](flow-build-project.png)

Expand Down
48 changes: 34 additions & 14 deletions docs/topics/debug-flow-with-idea.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,41 @@ This tutorial demonstrates how to create Kotlin Flow and debug it using IntelliJ

The tutorial assumes you have prior knowledge of the [coroutines](coroutines-guide.md) and [Kotlin Flow](flow.md#flows) concepts.

> Debugging works for `kotlinx-coroutines-core` version 1.3.8 or later.
>
{type="note"}

## Create a Kotlin flow

Create a Kotlin [flow](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/flow.html) 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](jvm-get-started.md#create-an-application).

2. 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!`.

3. Create the `simple()` function that returns a flow of three numbers:
1. Open a Kotlin project in IntelliJ IDEA. If you don't have a project, [create one](jvm-get-started.md#create-a-project).
2. To use the `kotlinx.coroutines` library in a Gradle project, add the following dependency to `build.gradle(.kts)`:

<tabs group="build-script">
<tab title="Kotlin" group-key="kotlin">

```kotlin
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:%coroutinesVersion%")
}
```

</tab>
<tab title="Groovy" group-key="groovy">

```groovy
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:%coroutinesVersion%'
}
```

</tab>
</tabs>

For other build systems, see instructions in the [`kotlinx.coroutines` README](https://github.com/Kotlin/kotlinx.coroutines#using-in-your-projects).

3. 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!`.

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

* Use the [`delay()`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/delay.html) 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()`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow-collector/emit.html) function.
Expand All @@ -36,7 +56,7 @@ Create a Kotlin [flow](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-cor
}
```

4. Change the code in the `main()` function:
5. Change the code in the `main()` function:

* Use the [`runBlocking()`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html) block to wrap a coroutine.
* Collect the emitted values using the [`collect()`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/collect.html) function.
Expand All @@ -53,7 +73,7 @@ Create a Kotlin [flow](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-cor
}
```

5. Build the code by clicking **Build Project**.
6. Build the code by clicking **Build Project**.

![Build an application](flow-build-project.png)

Expand Down Expand Up @@ -84,7 +104,7 @@ Create a Kotlin [flow](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-cor

## Add a concurrently running coroutine

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

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

Expand Down