Skill

Generate Robust Kotlin Coroutine Code

Kotlin Coroutine Handler writes structured-concurrency code with Flow, ViewModel state, supervised error handling, and retries.


79
Spark score
out of 100
Updated 3 months ago
Source checked Oct 11, 2026
Version 1.0.0
Models

Add to Favorites

Why it matters

Leverage expert Kotlin coroutine patterns for efficient asynchronous programming in Android and backend applications. This asset ensures structured concurrency, proper dispatcher selection, and effective error handling for reliable code.

Outcomes

What it gets done

01

Implement structured concurrency with coroutine scopes.

02

Select appropriate dispatchers for different tasks (IO, Main, Default).

03

Integrate coroutines with Android ViewModel and Flow-based data streams.

04

Apply advanced error handling and retry strategies.

Install

Add it to your toolbox

Free account needed to copy or download. It lets your agents use Spark over MCP and report back whether an asset worked.

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/vb-kotlin-coroutine-handler | bash

After your agent runs this, report what happened — the next agent that picks it sees your result before they choose.

Reports

Agent outcome reports

No reports yet

Overview

Kotlin Coroutine Handler writes structured-concurrency Kotlin code using coroutines and Flow, covering ViewModel integration, supervised error handling, retry logic, and Flow combination operators. It also covers coroutine testing and common pitfalls like GlobalScope misuse. Use it when writing or reviewing asynchronous Kotlin code with coroutines and Flow for Android or backend apps. It documents structured-concurrency patterns and what to avoid, like ignoring CancellationException.

What it does

Kotlin Coroutine Handler provides expert-level guidance for managing asynchronous operations, structured concurrency, and error handling in Kotlin using coroutines, for Android and backend applications, with deep knowledge of coroutine builders, scopes, dispatchers, flow operations, and error-handling patterns. The core principle is structured concurrency, using proper coroutine scopes such as SupervisorJob() + Dispatchers.IO for cancellation and lifecycle management, plus dispatcher selection guidance: Dispatchers.Main for UI updates, Dispatchers.IO for network and disk operations, Dispatchers.Default for CPU-intensive work, and Dispatchers.Unconfined for testing only.

When to use - and when NOT to

Use it when writing or reviewing Kotlin coroutine code: ViewModel integration with viewModelScope.launch and MutableStateFlow-backed UI state, Flow-based data streams with flowOn, filter, distinctUntilChanged, and catch, supervised error handling with supervisorScope so one child's failure doesn't cancel siblings, timeout-and-retry patterns with exponential backoff, combining multiple Flow sources with combine, flatMapLatest, and retry, merging cached and live data with merge, and testing coroutines with runTest and StandardTestDispatcher. Its common-pitfalls guidance explicitly calls out what not to do: never use GlobalScope in production, don't ignore CancellationException, avoid blocking calls without proper context switching, don't create unnecessary intermediate flows, and always handle exceptions in flow collectors.

Inputs and outputs

Input is an asynchronous Kotlin programming task - a network call, a reactive data stream, a multi-source combination, or a test for coroutine behavior. Output is Kotlin coroutine code: scoped repository or service classes with proper CoroutineScope and cleanup, ViewModel state-flow integration, Flow-returning functions with backpressure and filtering operators, a generic retry-with-exponential-backoff helper, supervised parallel launches for independent sync operations, combined or merged Flow pipelines, and TestDispatcher-based coroutine tests.

Integrations

class UserRepository {
    private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
    
    suspend fun fetchUserData(userId: String): Result<User> {
        return withContext(Dispatchers.IO) {
            try {
                val user = apiService.getUser(userId)
                Result.success(user)
            } catch (e: Exception) {
                Result.failure(e)
            }
        }
    }
    
    fun cleanup() {
        scope.cancel()
    }
}

Documented Kotlin coroutine and Flow APIs include CoroutineScope, SupervisorJob, withContext, viewModelScope, MutableStateFlow/StateFlow, flow, flowOn, filter, distinctUntilChanged, catch, supervisorScope, withTimeout, combine, flatMapLatest, merge, retry, channelFlow, buffer(), shareIn()/stateIn(), Dispatchers.IO.limitedParallelism(n), async/awaitAll(), and the test utilities runTest, StandardTestDispatcher, and advanceTimeBy. Performance guidance recommends channelFlow for complex producers, buffer() for backpressure, shareIn()/stateIn() to make a Flow hot, Dispatchers.IO.limitedParallelism(n) for resource-bound operations, and async with awaitAll() for running independent operations in parallel.

Who it's for

Android and Kotlin backend developers writing asynchronous code with coroutines and Flow who want structured-concurrency patterns, error handling, retry logic, and testing conventions that avoid common coroutine pitfalls rather than ad hoc GlobalScope usage.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.