coroutines-patterns

Structure asynchronous Kotlin code with Coroutines and Flow patterns.

61|17|Updated Feb 2, 2026
One-click install
npx skills add https://github.com/ahmed3elshaer/everything-claude-code-mobile --skill coroutines-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: coroutines-patterns
Source: https://github.com/ahmed3elshaer/everything-claude-code-mobile/tree/main/skills/coroutines-patterns
Command: npx skills add https://github.com/ahmed3elshaer/everything-claude-code-mobile --skill coroutines-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Kotlin developers often face scattered coroutine patterns and lifecycle-related bugs. This skill consolidates structured concurrency patterns to promote safe cancellation, predictable flow, and clear error handling.

Core Features & Use Cases

  • ViewModel-scoped coroutines with auto-cancellation
  • Flow patterns using StateFlow and SharedFlow
  • Error handling strategies in coroutines and supervisorScope
  • Use cases include UI data loading, event streams, and robust cancellation

Quick Start

Use the coroutines-patterns skill to implement coroutine-based asynchronous work in a ViewModel using viewModelScope and to manage lifecycle-aware flows in a Compose screen.

Frequently Asked Questions about coroutines-patterns

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

FAQPage Schema
How do I structure coroutines in a ViewModel to handle lifecycle cancellation?

Structured coroutines use viewModelScope to automatically cancel work when the ViewModel is destroyed. This ensures coroutines respect lifecycle boundaries and prevents memory leaks and orphaned tasks in Android apps.

What's the difference between StateFlow and SharedFlow for managing UI state?

StateFlow holds the latest state value and replays it to new collectors, making it ideal for UI state. SharedFlow broadcasts events without state retention, better for one-time events and multiple independent subscribers.

How do I handle errors in Kotlin coroutines without crashing the app?

Use supervisorScope to isolate failures so one failed child coroutine doesn't cancel siblings, and apply exception handlers in coroutine builders. This enables robust error handling across async operations and event streams.

Can I use coroutines with Compose screens to load data lifecycle-aware?

Yes. Compose integrates with coroutines via LaunchedEffect and lifecycle-aware scopes, letting you load data tied to composition lifecycle. Flow patterns ensure data updates and cancellation follow screen recomposition and navigation.

Why should I use Flow instead of callbacks for data streams in Android?

Flow provides structured, cancellable, composable data streams with built-in lifecycle awareness and error handling. Callbacks lack cancellation safety and require manual lifecycle management, making them prone to leaks and race conditions.

What separation of concerns do structured coroutines provide in a ViewModel?

Structured concurrency isolates async work in ViewModels using viewModelScope and Flow, keeping business logic separate from UI updates. This prevents tight coupling, simplifies testing, and centralizes cancellation and error handling.