What does this skill do?

The "Coroutines and Flows in Kotlin" Skill provides clear and robust patterns for implementing structured concurrency, reactive flows (Flow, StateFlow, SharedFlow), and coroutine testing in Android and Kotlin Multiplatform projects. It helps you avoid common anti-patterns—such as using GlobalScope or accidentally catching CancellationException—by generating idiomatic code that is safe throughout the lifecycle.

Parallel Loading with async
Execute tasks in parallel using `coroutineScope` and `async`, waiting for all of them to complete with error handling via `supervisorScope`.
Reactive State with StateFlow
Expose the state to the UI by converting flows to StateFlow using `stateIn` and `WhileSubscribed(5,000)` to handle configuration changes.
Unique Events with SharedFlow
Manage specific actions such as Snackbars or navigation, avoiding the need to rebuild after configuration changes.
Deterministic Testing with Turbine
Validate flow emissions using runTest and Turbine by controlling virtual time and test dispatchers.

Usage examples

🔀 Parallel charging
Create a ViewModel that loads data from two repositories in parallel using coroutines and exposes the state using StateFlow.
🔎 Debounce and retry
Implement a search with a 300-millisecond debounce, `distinctUntilChanged`, and retries with exponential backoff using Flow.
🔔 Events with SharedFlow
Design a pattern using SharedFlow to display snackbars and navigate without triggering events again after a configuration change.
🧪 Test with Turbine
Write a unit test using runTest and Turbine that validates the emissions from a StateFlow in a ViewModel.

Features

Structured Competition Patterns based on coroutineScope, supervisorScope, and viewModelScope that prevent memory leaks and uncontrolled cancellations.
State Management with StateFlow Converting flows to StateFlow using `stateIn` and `WhileSubscribed` to efficiently expose immutable state to the UI.
Error Handling and Retry Mechanisms Strategies using catch, retryWhen, and exponential backoff to handle network failures and transient operations.
Cooperative Cancellation Using ensureActive() and finally blocks to release resources and ensure cleanup in canceled coroutines.
Deterministic testing Tests using runTest and Turbine that monitor virtual time and dispatchers to validate flow emissions.

Frequently asked questions

No. This skill generates Kotlin code directly. To run the examples, you need the standard coroutine dependencies (kotlinx-coroutines-core) and Turbine for testing.
Yes. Structured concurrency patterns and reactive flows are valid for both Android and Kotlin Multiplatform (KMP) projects.
Yes. The skill explicitly prohibits the use of GlobalScope, the accidental catching of CancellationException, and the creation of flows without `remember` in @Composable functions.
Yes. The generated code is idiomatic and serves as a solid foundation that you can adapt to your project's specific architecture.
Coroutines and Flows in Kotlin — Structured Concurrency with Claude AI

¿Prefieres escuchar el contenido? Genera la narración de audio con un clic.