kotlin-concurrency-expert

Review Android Kotlin coroutines for leaks races lifecycle issues and remediate them.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill addresses common issues with Kotlin Coroutines in Android development, ensuring robust, lifecycle-aware, and thread-safe asynchronous operations.

Core Features & Use Cases

  • Concurrency Review: Analyzes Kotlin Coroutines usage for potential bugs, memory leaks, and performance bottlenecks.
  • Bug Fixes: Remediates issues like ANRs, main thread blocking, and lifecycle collection problems.
  • Best Practice Implementation: Enforces structured concurrency, proper scoping, and testable dispatcher injection.
  • Use Case: A developer is experiencing ANRs in their Android app due to blocking the main thread. This Skill can identify the problematic coroutine, refactor it to use Dispatchers.IO with withContext, and ensure it's properly scoped to the ViewModel's lifecycle.

Quick Start

Use the kotlin-concurrency-expert skill to review the coroutine usage in the provided Android code snippet.

Frequently Asked Questions about kotlin-concurrency-expert

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

FAQPage Schema
How do I fix ANRs and main thread blocking caused by Kotlin Coroutines in my Android app?

To fix ANRs and main thread blocking in Kotlin Coroutines, identify problematic coroutines and refactor them using `Dispatchers.IO` with `withContext`. Ensure operations are properly scoped to the ViewModel's lifecycle to prevent main thread freezing and application unresponsiveness.

What is structured concurrency in Kotlin and how does it prevent memory leaks?

Structured concurrency in Kotlin prevents memory leaks by enforcing proper scoping and cooperative cancellation. Coroutines are tied to a specific lifecycle scope, ensuring they are automatically cancelled when their parent scope is destroyed, avoiding orphaned background tasks.

How do I safely collect Kotlin Flows in an Android app without causing lifecycle crashes?

To safely collect Kotlin Flows without causing lifecycle crashes, apply lifecycle-aware collection methods. This ensures flow collection only occurs when the Android lifecycle component is active, suspending collection during stopped states to prevent crashes and save resources.

Can I convert existing callback APIs to Kotlin Coroutines and Flows for asynchronous operations?

Yes, you can convert existing callback APIs to Kotlin Coroutines and Flows. This process involves wrapping callback-based asynchronous operations into suspend functions or Flow producers, applying best practices to ensure thread safety and cooperative cancellation during the conversion.

Why are my Kotlin Coroutines causing race conditions and incorrect state exposure in Android?

Kotlin Coroutines cause race conditions and incorrect state exposure when multiple threads access shared mutable state without synchronization. Remediating this requires applying thread safety best practices, using proper dispatcher injection, and ensuring state updates are confined to a single thread.

What is testable dispatcher injection in Kotlin Coroutines and when should I use it?

Testable dispatcher injection in Kotlin Coroutines is the practice of passing dispatchers via parameters rather than hardcoding them. You should use it to write deterministic unit tests, allowing you to easily swap `Dispatchers.IO` for a test dispatcher to control execution.