Agent skill
Kotlin Coroutines & Flow
Structured Concurrency, Flow patterns, and Asynchronous programming standards.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/data/coroutines
Metadata
Additional technical details for this skill
- labels
-
kotlin coroutines flow async concurrency
- triggers
-
{ "files": [ "**/*.kt" ], "keywords": [ "suspend", "launch", "async", "flow", "StateFlow", "SharedFlow", "Dispatchers" ] }
SKILL.md
Kotlin Coroutines & Flow
Priority: P1 (HIGH)
Standard for safe, structured asynchronous programming.
Implementation Guidelines
- Structured Concurrency: Always launch coroutines within a
CoroutineScope(e.g.,viewModelScope,lifecycleScope). Never useGlobalScope. - Dispatchers: Inject
DispatcherProviderorCoroutineDispatcherto simplify testing. Do not hardcodeDispatchers.IOin classes. - Suspend Functions: Mark blocking/long-running operations as
suspend. They should be "main-safe" (handle their own context switching). - Flow: Prefer
StateFlow(state holder) andSharedFlow(events) overLiveData. - Collection: Use
collectLatestfor restartable upstream updates. UseflowWithLifecyclein UI. - Error Handling: Use
CoroutineExceptionHandlerfor top-level launch,try/catchfor code blocks within coroutines.
Anti-Patterns
- GlobalScope: Leaks memory and breaks structure.
- Blocking Calls: Never use
Thread.sleepor blocking I/O in coroutines. Usedelayor proper suspend functions. - Async/Await Abuse: Don't use
asyncunless you need parallel execution. Use linear code inlaunch. - Mutable State in Flow: Don't expose
MutableStateFlowpublicly. Cast toStateFlow.
Code
For detailed ViewModel + StateFlow and Parallel Async examples:
references/advanced-patterns.md
// Structured Scope + Main-safe Suspend
viewModelScope.launch {
val data = withContext(Dispatchers.IO) { repo.fetch() }
_state.value = UiState.Success(data)
}
Related Topics
best-practices | language | android
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?