Kotlin Coroutines & Flows
Structured concurrency, Flow-based reactive streams, and coroutine testing for Android and KMP. Detalhes em references/.
When to Activate
- Writing async code with Kotlin coroutines
- Using Flow, StateFlow, or SharedFlow for reactive data
- Handling concurrent operations (parallel loading, debounce, retry)
- Testing coroutines and Flows
- Managing coroutine scopes and cancellation
Core Principles
- Structured concurrency only —
viewModelScope/LaunchedEffect, never GlobalScope
- Parallel via
coroutineScope + async — supervisorScope when siblings must survive
- StateFlow for UI state —
WhileSubscribed(5_000); SharedFlow for one-time events
- Right dispatcher per job — Default for CPU, IO for JVM/Android IO, Main for UI
- Cooperative cancellation —
ensureActive() in loops, try/finally for cleanup
Example
suspend fun loadDashboard(): Dashboard = coroutineScope {
val items = async { itemRepository.getRecent() }
val stats = async { statsRepository.getToday() }
Dashboard(items = items.await(), stats = stats.await())
}
References
references/structured-concurrency.md — scope hierarchy, parallel decomposition, supervisorScope
references/flows.md — cold Flow, StateFlow, combine, operators, SharedFlow events
references/dispatchers-cancellation.md — dispatchers, KMP notes, cooperative cancellation
references/testing.md — Turbine, TestDispatcher, fakes, anti-patterns, related skills
Checklist
1---2name: kotlin-coroutines-flows3description: "Use when kotlin Coroutines and Flow patterns for Android and KMP — structured concurrency, Flow operators, StateFlow, error handling, and testing. Triggers on \"kotlin-coroutines-flows\", \"kotlin coroutines flows\", \"flows\"."4metadata:5 source: https://skillmd.com/skills/majinmagros/kotlin-coroutines-flows6 origin: ECC7---89# Kotlin Coroutines & Flows1011Structured concurrency, Flow-based reactive streams, and coroutine testing for Android and KMP. Detalhes em `references/`.1213## When to Activate1415- Writing async code with Kotlin coroutines16- Using Flow, StateFlow, or SharedFlow for reactive data17- Handling concurrent operations (parallel loading, debounce, retry)18- Testing coroutines and Flows19- Managing coroutine scopes and cancellation2021## Core Principles22231. **Structured concurrency only** — `viewModelScope`/`LaunchedEffect`, never `GlobalScope`242. **Parallel via `coroutineScope` + `async`** — `supervisorScope` when siblings must survive253. **StateFlow for UI state** — `WhileSubscribed(5_000)`; SharedFlow for one-time events264. **Right dispatcher per job** — Default for CPU, IO for JVM/Android IO, Main for UI275. **Cooperative cancellation** — `ensureActive()` in loops, `try/finally` for cleanup2829## Example3031```kotlin32suspend fun loadDashboard(): Dashboard = coroutineScope {33 val items = async { itemRepository.getRecent() }34 val stats = async { statsRepository.getToday() }35 Dashboard(items = items.await(), stats = stats.await())36}37```3839## References4041- `references/structured-concurrency.md` — scope hierarchy, parallel decomposition, supervisorScope42- `references/flows.md` — cold Flow, StateFlow, combine, operators, SharedFlow events43- `references/dispatchers-cancellation.md` — dispatchers, KMP notes, cooperative cancellation44- `references/testing.md` — Turbine, TestDispatcher, fakes, anti-patterns, related skills4546## Checklist4748- [ ] No `GlobalScope`; every launch tied to a lifecycle scope49- [ ] UI state is StateFlow with `WhileSubscribed`; events are SharedFlow50- [ ] Immutable copies in `_state.update`, never caught `CancellationException`51- [ ] Tests use `runTest` + Turbine/`advanceUntilIdle` with fake repositories52- [ ] KMP code avoids JVM-only `Dispatchers.IO`