技能 kotlin-coroutines-expert
📦

kotlin-coroutines-expert

安全

Master Kotlin Coroutines & Flow

Kotlin Coroutines and Flow can be complex to master. This skill provides expert patterns for structured concurrency, error handling, and testing asynchronous code.

支援: Claude Codex Code(CC)
🥉 73 青銅
1

下載技能 ZIP

2

在 Claude 中上傳

前往 設定 → 功能 → 技能 → 上傳技能

3

開啟並開始使用

測試它

正在使用「kotlin-coroutines-expert」。 How do I run two API calls in parallel and handle errors independently?

預期結果:

Use supervisorScope to run async tasks where failure of one does not cancel the others. Example: val task1 = async { api.fetchA() }; val task2 = async { api.fetchB() }; Both can fail independently without affecting each other.

正在使用「kotlin-coroutines-expert」。 When should I use StateFlow vs SharedFlow?

預期結果:

Use StateFlow for UI state that needs to be retained and always has a current value. Use SharedFlow for one-time events like navigation or showing toast messages.

安全審計

安全
v1 • 2/25/2026

Static analysis flagged 26 potential issues (24 external_commands, 2 weak cryptographic). Manual review confirms all are FALSE POSITIVES. The 'external_commands' detections are markdown backtick formatting (e.g., `coroutineScope`) not shell execution. The 'weak cryptographic' detections are misidentified content. This is a legitimate Kotlin Coroutines documentation skill with no security risks.

1
已掃描檔案
101
分析行數
0
發現項
1
審計總數
未發現安全問題
審計者: claude

品質評分

38
架構
100
可維護性
85
內容
50
社群
100
安全
83
規範符合性

你能建構什麼

Backend Developer

Implement async API calls and parallel data fetching in Kotlin microservices

Android Developer

Handle async UI updates and reactive data streams in Android apps

Code Reviewer

Review coroutine code for proper error handling and structured concurrency patterns

試試這些提示

Basic Coroutine Setup
Show me how to set up a CoroutineScope for parallel API calls in Kotlin
Flow Error Handling
How do I handle errors in a Kotlin Flow pipeline?
Coroutine Testing
Write a unit test for a suspending function using TestScope
Cancellation Handling
Explain how to properly handle coroutine cancellation without catching CancellationException

最佳實務

  • Always use Dispatchers.IO for blocking I/O operations to avoid blocking the main thread
  • Cancel coroutine scopes when they are no longer needed (e.g., in ViewModel.onCleared)
  • Use TestScope and runTest for deterministic coroutine testing with virtual time

避免

  • Avoid GlobalScope - it breaks structured concurrency and can lead to memory leaks
  • Do not catch CancellationException unless you rethrow it - this breaks cooperative cancellation
  • Avoid using suspendCoroutine or suspendCancellableCoroutine when higher-level APIs suffice

常見問題

What is structured concurrency in Kotlin?
Structured concurrency ensures coroutines are tied to a lifecycle scope. When the scope cancels, all its child coroutines are cancelled, preventing leaks.
When should I use supervisorScope vs coroutineScope?
Use coroutineScope when you want any failure to cancel all children. Use supervisorScope when you want children to fail independently without affecting siblings.
How do I test coroutines?
Use TestScope and runTest from kotlinx.coroutines.test. Inject TestDispatcher to control virtual time for deterministic testing.
What is the difference between Flow and StateFlow?
Flow is cold - it produces values only when collected. StateFlow is hot - it retains a current value and emits to all collectors.
How do I handle exceptions in Flow?
Use catch operator at the end of the pipeline, or use try-catch inside the flow builder. For retrying, use retry or retryWhen operators.
What are common causes of coroutine memory leaks?
Using GlobalScope, not cancelling scopes when done, capturing context incorrectly, or holding references to actors that are never completed.

開發者詳情

檔案結構

📄 SKILL.md