npx skills add ...
npx skills add affaan-m/ecc --skill kotlin-coroutines-flows
Kotlin Coroutines and Flow patterns for Android and KMP — structured concurrency, Flow operators, StateFlow, error handling, and testing. Use when writing coroutines or Flow code on Android or KMP, or debugging cancellation and concurrency.
npx skills add affaan-m/ecc --skill kotlin-coroutines-flows
Patterns for structured concurrency, Flow-based reactive streams, and coroutine testing in Android and Kotlin Multiplatform projects.
Always use structured concurrency — never GlobalScope:
Use coroutineScope + async for parallel work:
Use supervisorScope when child failures should not cancel siblings:
WhileSubscribed(5_000) keeps the upstream active for 5 seconds after the last subscriber leaves — survives configuration changes without restarting.
In KMP, use Dispatchers.Default and Dispatchers.Main (available on all platforms). Dispatchers.IO is JVM/Android only — use Dispatchers.Default on other platforms or provide via DI.
Long-running loops must check for cancellation:
GlobalScope — leaks coroutines, no structured cancellationinit {} without a scope — use viewModelScope.launchMutableStateFlow with mutable collections — always use immutable copies: _state.update { it.copy(list = it.list + newItem) }CancellationException — let it propagate for proper cancellationflowOn(Dispatchers.Main) to collect — collection dispatcher is the caller's dispatcherFlow in @Composable without remember — recreates the flow every recompositionSee skill: compose-multiplatform-patterns for UI consumption of Flows.
See skill: android-clean-architecture for where coroutines fit in layers.