Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
ahmed3elshaer avatar

Coroutines Patterns

  • 26 installs
  • 60 repo stars
  • Updated June 14, 2026
  • ahmed3elshaer/everything-claude-code-mobile

coroutines-patterns is a Claude Code skill providing Kotlin Coroutines and Flow patterns for structured concurrency in mobile apps.

About

coroutines-patterns is a Claude Code skill with Kotlin Coroutines and Flow reference patterns for structured concurrency. A developer uses it when writing async code in an Android or Kotlin app to choose the right coroutine scope, dispatcher, and Flow type. It flags anti-patterns such as GlobalScope that cause memory leaks.

  • Structured-concurrency scope guidance (viewModelScope, lifecycleScope, avoid GlobalScope)
  • Dispatcher selection for Main, IO, and Default work
  • StateFlow, SharedFlow, and lifecycle-aware collection patterns

Coroutines Patterns by the numbers

  • 26 all-time installs (skills.sh)
  • Ranked #708 of 1,039 Mobile Development skills by installs in the Skillselion catalog
  • Data as of Aug 4, 2026 (Skillselion catalog sync)
At a glance

coroutines-patterns capabilities & compatibility

Capabilities
kotlin coroutines · flow patterns · structured concurrency
Use cases
api development
From the docs

What coroutines-patterns says it does

Kotlin Coroutines and Flow patterns for structured concurrency, error handling, and async operations.
SKILL.md
GlobalScope.launch { } // Never cancelled, memory leaks
SKILL.md
npx skills add https://github.com/ahmed3elshaer/everything-claude-code-mobile --skill coroutines-patterns

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs26
repo stars60
Last updatedJune 14, 2026
Repositoryahmed3elshaer/everything-claude-code-mobile

What it does

Write correct Kotlin coroutine and Flow code with the right scope, dispatcher, and structured concurrency.

Who is it for?

Android and Kotlin developers writing async code with coroutines and Flow

Skip if: iOS Swift concurrency (async/await)

When should I use this skill?

You are writing coroutine scopes, dispatchers, or Flow-based state in a Kotlin app

By the numbers

  • Covers 3 dispatchers (Main, IO, Default)
  • Contrasts StateFlow vs SharedFlow

Files

SKILL.mdMarkdownGitHub ↗

Coroutines Patterns

Structured concurrency for Kotlin.

Coroutine Scopes

// ✅ ViewModel scope (auto-cancelled)
class HomeViewModel : ViewModel() {
    fun loadData() {
        viewModelScope.launch {
            // Cancelled when ViewModel cleared
        }
    }
}

// ✅ Lifecycle scope
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycleScope.launch {
            // Cancelled when lifecycle destroyed
        }
    }
}

// ❌ AVOID: GlobalScope
GlobalScope.launch { }  // Never cancelled, memory leaks

Dispatchers

// Main - UI operations
withContext(Dispatchers.Main) {
    textView.text = "Updated"
}

// IO - Network, disk
withContext(Dispatchers.IO) {
    api.fetchData()
    database.query()
}

// Default - CPU intensive
withContext(Dispatchers.Default) {
    list.sortedBy { it.score }
}

Flow Patterns

// StateFlow for UI state
private val _state = MutableStateFlow(HomeState())
val state: StateFlow<HomeState> = _state.asStateFlow()

// SharedFlow for events
private val _events = MutableSharedFlow<Event>()
val events: SharedFlow<Event> = _events.asSharedFlow()

// Collect with lifecycle
@Composable
fun HomeScreen(viewModel: HomeViewModel) {
    val state by viewModel.state.collectAsStateWithLifecycle()
}

Flow Operators

flow
    .filter { it.isActive }
    .map { transform(it) }
    .distinctUntilChanged()
    .debounce(300)
    .catch { emit(fallback) }
    .collect { process(it) }

Error Handling

// Try-catch in coroutine
viewModelScope.launch {
    try {
        val result = repository.fetchData()
        _state.value = Success(result)
    } catch (e: Exception) {
        _state.value = Error(e.message)
    }
}

// supervisorScope - siblings don't cancel
supervisorScope {
    launch { task1() }  // Failure doesn't cancel task2
    launch { task2() }
}

Cancellation

// Cooperative cancellation
suspend fun processItems(items: List<Item>) {
    items.forEach { item ->
        ensureActive()  // Check cancellation
        process(item)
    }
}

// CancellationException handling
try {
    coroutineWork()
} catch (e: CancellationException) {
    throw e  // Don't swallow!
} catch (e: Exception) {
    handleError(e)
}

---

Remember: Structured concurrency = lifecycle-bound, cancellable, debuggable.

Related skills

FAQ

Why avoid GlobalScope?

The skill notes GlobalScope is never cancelled and causes memory leaks; it recommends viewModelScope or lifecycleScope instead.

Which dispatcher for network calls?

Dispatchers.IO for network and disk work, Dispatchers.Default for CPU-intensive work, and Dispatchers.Main for UI updates.

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.