界面已本地化;技能介绍与说明文档按所选语言由 AI 自动翻译,代码、命令、路径和专业术语保留原文,以原作者文档为准。
探索技能
开发与工程 · SKILL.md

kotlin-coroutines

kotlin-coroutines

【开发与工程】Write safe, structured concurrent code with Kotlin Coroutines. Use for suspend functions, coroutine scopes, cancellation, and coroutine leaks; defer Java Virtual Threads, Android WorkManager/Fragment lifecycle, Compose state hoisting, and Detekt/tooling configuration.

独立 GitHub 快照 · 自动分类 · 以原作者文档为准

技能说明

GitHub

name: kotlin-coroutines description: Write safe, structured concurrent code with Kotlin Coroutines. Use for suspend functions, coroutine scopes, cancellation, and coroutine leaks; defer Java Virtual Threads, Android WorkManager/Fragment lifecycle, Compose state hoisting, and Detekt/tooling configuration. metadata: triggers: files: - '**/*.kt' keywords: - suspend - CoroutineScope - launch - async - Flow - StateFlow - SharedFlow - viewModelScope - GlobalScope - Dispatchers - isActive - yield - runBlocking

Kotlin Coroutines Expert

Priority: P0 (CRITICAL)

Role: Concurrency Expert. Prioritize safety and cancellation support.

Implementation Guidelines

  • Scope: Use viewModelScope (Android) or structured coroutineScope.
  • Dispatchers: Inject dispatchers; never hardcode Dispatchers.IO.
  • Flow: Use StateFlow for state, SharedFlow for events.
  • Exceptions: Use runCatching or CoroutineExceptionHandler.

Concurrency Checklist (Mandatory)

  • Cancellation: loops check isActive or call yield()?
  • Structured: No GlobalScope? All children joined/awaited?
  • Context: Dispatchers.Main used for UI updates?
  • Leaks: scopes cancelled in onCleared / onDestroy?

Anti-Patterns

  • No GlobalScope: It leaks. Use structured concurrency.
  • No Async without Await: Don't async { ... } without await().
  • No Blocking: Never runBlocking in prod code (only tests).

References

我的收藏