coroutines-patterns

Provide Kotlin Coroutines patterns for Android StateFlow, SharedFlow, and structured concurrency.

1|1|Updated Feb 5, 2026
One-click install
npx skills add https://github.com/maichanchinh/claude-skills --skill coroutines-patterns-maichanchinh
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: coroutines-patterns
Source: https://github.com/maichanchinh/claude-skills/tree/main/skills/coroutines-patterns
Command: npx skills add https://github.com/maichanchinh/claude-skills --skill coroutines-patterns-maichanchinh

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

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.

Frequently Asked Questions about coroutines-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I manage UI state with Kotlin Coroutines in an Android ViewModel?

To manage UI state with Kotlin Coroutines in an Android ViewModel, use StateFlow to expose data streams. This pattern leverages structured concurrency within viewModelScope to ensure lifecycle-aware state emission and automatic cancellation.

What is the best way to handle one-time events in Android using Coroutines?

The best way to handle one-time events like navigation in Android Coroutines is using SharedFlow. SharedFlow allows emitting events to multiple consumers without storing state, ensuring that navigation or snackbar events are processed only once.

How do I test Kotlin Coroutines and StateFlow in Android applications?

To test Kotlin Coroutines and flows in Android, use the runTest API and test dispatchers. This approach replaces real dispatchers with test scopes, allowing you to control virtual time and verify StateFlow emissions and suspend functions deterministically.

Why do my Kotlin Coroutines leak memory or fail to cancel in Android?

Kotlin Coroutines leak memory when not properly scoped within structured concurrency boundaries. Ensure you use viewModelScope or custom CoroutineScope with SupervisorJob, and apply cooperative cancellation using ensureActive or yield inside long-running tasks.

How do I handle errors and retries in Kotlin Flow streams?

Handle errors and retries in Kotlin Flow streams by applying the catch operator for exception handling and combining it with retry mechanisms. This ensures robust data streaming and recovery from transient network failures in repository patterns.

Can I use Kotlin Coroutines with complex data streams and operators in Android?

Yes, you can use Kotlin Coroutines with complex data streams in Android by chaining Flow operators. Operators like debounce, flatMapLatest, and combine allow you to transform, filter, and merge multiple asynchronous data sources efficiently.