What problem does it solve?
This Skill addresses the complexities of asynchronous programming in Android development by providing best practices and patterns for using Kotlin Coroutines, StateFlow, SharedFlow, and structured concurrency.
Core Features & Use Cases
- State Management: Efficiently manage UI state with
StateFlow in ViewModels.
- Event Handling: Implement one-time events and navigation using
SharedFlow.
- Asynchronous Operations: Structure repository patterns with
suspend functions and Flow streams.
- Error Handling: Implement robust error handling with
Result wrappers and retry mechanisms.
- Structured Concurrency: Utilize
viewModelScope, SupervisorJob, and custom CoroutineScope for reliable background tasks.
- Flow Operators: Apply common operators like
debounce, flatMapLatest, combine, and catch for complex data streams.
- Cancellation: Ensure cooperative cancellation with
ensureActive and yield.
- Dispatchers: Correctly use
Dispatchers.IO, Dispatchers.Default, and Dispatchers.Main.
- Testing: Leverage
runTest and test dispatchers for coroutine testing.
Quick Start
Use the coroutines-patterns skill to implement StateFlow for managing UI state in an Android ViewModel.