android-coroutines

Enforce structured concurrency and lifecycle-safe Kotlin Coroutines on Android.

10.4k|519|Updated Apr 15, 2023
One-click install
npx skills add https://github.com/maxrave-dev/SimpMusic --skill android-coroutines-maxrave-dev
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-coroutines
Source: https://github.com/maxrave-dev/SimpMusic/tree/main/.claude/skills/android-coroutines
Command: npx skills add https://github.com/maxrave-dev/SimpMusic --skill android-coroutines-maxrave-dev

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides authoritative rules and patterns for writing production-quality Kotlin Coroutines code on Android, ensuring structured concurrency, lifecycle safety, and adherence to modern best practices.

Core Features & Use Cases

  • Asynchronous Operations: Efficiently handle background tasks, network calls, and database operations.
  • Reactive Programming: Implement Flow, StateFlow, and SharedFlow for real-time data updates.
  • Lifecycle Management: Safely manage coroutine scopes tied to Android lifecycles (viewModelScope, lifecycleScope).
  • Error Handling & Testing: Robust strategies for managing exceptions and writing unit tests with runTest.
  • Use Case: When building an Android app, use this skill to implement a ViewModel that fetches user data from an API asynchronously and displays it safely, ensuring no memory leaks or crashes occur due to lifecycle changes.

Quick Start

Use the android-coroutines skill to implement a suspend function for fetching data from an API, ensuring it's main-safe and uses an injected Dispatcher.

Frequently Asked Questions about android-coroutines

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

FAQPage Schema
How do I use Kotlin Coroutines on Android to fetch data from an API without blocking the main thread?

To use Kotlin Coroutines on Android for asynchronous API data fetching without blocking the main thread, implement suspend functions with an injected Dispatcher. This approach ensures your network operations remain main-safe and adhere to structured concurrency rules.

What is the best way to collect Flow in Android lifecycle-aware components?

The best way to collect Flow in Android lifecycle-aware components is by using repeatOnLifecycle. This method safely manages reactive streams like StateFlow and SharedFlow, ensuring collection automatically pauses and resumes with lifecycle changes to prevent memory leaks.

Why should I avoid using GlobalScope in Android coroutines?

You should avoid using GlobalScope in Android coroutines because it breaks structured concurrency and causes memory leaks. Instead, manage scopes via viewModelScope or lifecycleScope to ensure coroutine operations are properly canceled when their associated Android component is destroyed.

How do I handle errors in Kotlin Coroutines on Android?

To handle errors in Kotlin Coroutines on Android, implement robust strategies using CoroutineExceptionHandler. This allows you to gracefully manage exceptions during asynchronous operations, ensuring background tasks or network calls fail safely without crashing the application.

Can I use runTest for testing suspend functions and ViewModel coroutines?

Yes, you can use runTest with TestDispatcher for testing suspend functions and ViewModel coroutines. This approach provides authoritative rules for writing unit tests, allowing you to validate asynchronous operations and lifecycle integration without relying on real time delays.

When do I need callbackFlow instead of a standard suspend function in Kotlin?

You need callbackFlow in Kotlin when integrating existing callback-based APIs into reactive streams. It bridges asynchronous operations by converting listener-based data sources into a Flow, enabling seamless integration with modern StateFlow or SharedFlow reactive programming patterns.