android-coroutines-expert
Skill11 starsChanged 8 months agoMITView on GitHub
Use to enforce safe concurrency, correct Dispatcher usage, and proper Channel vs StateFlow patterns in ViewModels.
In this repository40 other files it gives its agents.
- .github/copilot-instructions.mdCopilot instructions
- android-accessibilitySkill
- android-adaptive-layoutsSkill
- android-adb-terminalSkill
- android-background-executionSkill
- android-bluetooth-bleSkill
- android-build-verificationSkill
- android-camerax-mlkitSkill
- android-ci-cd-expertSkill
- android-clean-architectureSkill
- android-code-review-expertSkill
- android-custom-drawing-canvasSkill
- android-design-system-m3Skill
- android-design-to-codeSkill
- android-detekt-expertSkill
- android-enterprise-securitySkill
- android-gemini-nanoSkill
- android-google-workspace-expertSkill
- android-gradle-expertSkill
- android-hardware-testingSkill
- android-jetpack-composeSkill
- android-kmp-compose-uiSkill
- android-kmp-platform-channelsSkill
- android-kmp-sharedSkill
- android-ktlint-formattingSkill
- android-lint-and-qualitySkill
- android-local-stt-ttsSkill
- android-location-mapsSkill
- android-macrobenchmarkSkill
- android-media3-exoplayerSkill
- android-media-and-image-expertSkill
- android-motion-animationsSkill
- android-navigation-expertSkill
- android-offline-firstSkill
- android-performance-auditSkill
- android-rag-localSkill
- android-screenshot-testingSkill
- android-security-hardenedSkill
- android-unit-testingSkill
- android-wear-os-composeSkill
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 0 are distinctive, and 6 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
6
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 30
UI STATE: Use MutableStateFlow.
- Very specific: line 31
UI EVENTS: Use an un-buffered Channel exported as a Flow.
- Very specific: line 55
Rule: If you are using Retrofit or Room, they are naturally "main-safe", so viewModelScope.launch { } is fine.
- Very specific: line 56
Rule: If you are doing manual file I/O, heavy JSON parsing, or bitmap manipulation, you MUST switch to Dispatchers.IO using …
- Very specific: line 57
Rule: If you are doing CPU-heavy sorting or filtering on massive lists, use Dispatchers.Default.
- Very specific: line 60
If an agent executes multiple independent API requests inside a viewModelScope.launch, and one fails, it cancels the entire scope/parent …
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Coroutines & Concurrency Mastery ⚡🧵
A strict rulebook to prevent thread-blocking, memory leaks, and lost UI events in modern Android applications.
⚡ When to Use
- ViewModel Logic: Launching coroutines for network/database requests.
- One-Time UI Events: Showing Snackbars, Toasts, or Navigation triggers.
- Parallel Work: Running multiple non-blocking tasks.
🚦 Rule 1: The UI Event Dilemma (StateFlow vs Channel)
You must explicitly distinguish between UI State (things that persist on rotation) and UI Events (things that happen once, like a Toast or Navigation).
- UI STATE: Use
MutableStateFlow. - UI EVENTS: Use an un-buffered
Channelexported as aFlow. NEVER useSharedFlowwithreplay = 0orMutableStateFlowfor one-time events, because if the app rotates, the event might be dropped or replayed twice.
The Golden Implementation:
class MyViewModel : ViewModel() {
// 1. UI STATE
private val _uiState = MutableStateFlow(MyState())
val uiState = _uiState.asStateFlow()
// 2. ONE-TIME EVENTS
private val _uiEvent = Channel<MyAction>()
val uiEvent = _uiEvent.receiveAsFlow()
fun triggerSnackbar() {
viewModelScope.launch {
_uiEvent.send(MyAction.ShowSnackbar)
}
}
}🚥 Rule 2: Dispatcher Discipline
Agents frequently launch heavy database or network calls on the default dispatcher (Dispatchers.Main inside a viewModelScope.launch).
- Rule: If you are using Retrofit or Room, they are naturally "main-safe", so
viewModelScope.launch { }is fine. - Rule: If you are doing manual file I/O, heavy JSON parsing, or bitmap manipulation, you MUST switch to
Dispatchers.IOusingwithContext(Dispatchers.IO) { ... }. - Rule: If you are doing CPU-heavy sorting or filtering on massive lists, use
Dispatchers.Default.
🛑 Rule 3: Structured Concurrency
If an agent executes multiple independent API requests inside a viewModelScope.launch, and one fails, it cancels the entire scope/parent job!
The Anti-Pattern:
viewModelScope.launch {
// If ApiA fails, ApiB is cancelled!
val resultA = apiA.fetch()
val resultB = apiB.fetch()
}The Architect's Standard (supervisorScope or async):
viewModelScope.launch {
// Both attempt to run. If A fails, B continues successfully.
supervisorScope {
val deferredA = async { apiA.fetch() }
val deferredB = async { apiB.fetch() }
try { val a = deferredA.await() } catch (e: Exception) {}
try { val b = deferredB.await() } catch (e: Exception) {}
}
}🔋 Rule 4: collectAsStateWithLifecycle
As enforced in our Code Review checklists, NEVER use collectAsState() in Compose UI. Always use collectAsStateWithLifecycle() to ensure Coroutines pause fetching when the screen goes to the background.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

