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.

By affaan-m · 2,970 installs

npx skills add affaan-m/ecc --skill kotlin-coroutines-flows

Source repository · Upstream listing

Kotlin Coroutines & Flows Patterns for structured concurrency, Flow based reactive streams, and coroutine testing in Android and Kotlin Multiplatform projects. 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 Structured Concurrency Scope Hierarchy Always use structured concurrency — never GlobalScope : Parallel Decomposition Use coroutineScope + async for parallel work: SupervisorScope Use supervisorScope when child failures should not cancel siblings: Flow Patterns Cold Flow — One Shot to Stream Conversion StateFlow for UI State WhileSubscribed(5 000) keeps the upstream active for 5 seconds after the last subscriber leaves — survives configuration changes without restarting. Combining Multiple Flows Flow Operators SharedFlow for One Time Events Dispatchers 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. Cancellation Cooperative Cancellation Long running loops must check for cancellation: Cleanup with try/finally Testing Testing StateFlow with Turbine Testing with TestDispatcher Faking Flows Anti Patterns to Avoid Using GlobalScope — leaks coroutines, no structured cancellation Collecting Flows in init {} without a scope — use viewModelScope.launch Using MutableStateFlow with mutable collections — always use immutable copies: state.update { it.copy(list = it.list + newItem) } Catching CancellationException — let it propagate for proper cancellation Using flowOn(Dispatchers.Main) to collect — collection dispatcher is the caller's dispatcher Creating Flow in @Composable without remember — recreates the flow every recomposition References See skill: compose multiplatform patterns for UI consumption of Flows. See skill: android clean architecture for where coroutines fit in layers.