Skill

Generate Robust Kotlin Coroutine Code

Expert skill for Kotlin coroutines, structured concurrency, Flow operations, and async patterns in Android and backend applications with lifecycle-aware error


79
Spark score
out of 100
Updated 7 months ago
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

Run in your project directory:

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

Overview

Kotlin Coroutine Handler

This skill provides expert guidance on Kotlin coroutines for asynchronous programming, structured concurrency, and reactive data flows in Android and backend applications. It covers coroutine builders, scopes, dispatchers (Main, IO, Default), Flow operations, error handling patterns, and lifecycle management to help developers write non-blocking, cancellation-aware concurrent code. Use this skill when building Android ViewModels with StateFlow, implementing repository patterns with proper scope management, creating reactive data streams with Flow operators (combine, flatMapLatest, retry), handling supervised error propagation across multiple concurrent tasks, or implementing timeout and exponential backoff retry logic. Use it when you need to coordinate multiple asynchronous data sources or optimize coroutine performance with backpressure and hot flows. Do NOT use this skill if you need blocking synchronous operations without context switching, or if you're working in e

What it does

This skill provides expert guidance on Kotlin coroutines for asynchronous programming, structured concurrency, and reactive data flows in Android and backend applications. It covers coroutine builders, scopes, dispatchers (Main, IO, Default), Flow operations, error handling patterns, and lifecycle management to help developers write non-blocking, cancellation-aware concurrent code.

When to use - and when NOT to

Use this skill when building Android ViewModels with StateFlow, implementing repository patterns with proper scope management, creating reactive data streams with Flow operators (combine, flatMapLatest, retry), handling supervised error propagation across multiple concurrent tasks, or implementing timeout and exponential backoff retry logic. Use it when you need to coordinate multiple asynchronous data sources or optimize coroutine performance with backpressure and hot flows.

Do NOT use this skill if you need blocking synchronous operations without context switching, or if you're working in environments where Kotlin coroutines are not available. Avoid using GlobalScope patterns or ignoring CancellationException as the skill explicitly warns against these anti-patterns.

Inputs and outputs

You provide your specific coroutine implementation challenge: ViewModel integration needs, repository async patterns, Flow-based data stream requirements, error handling strategies (supervised jobs, timeout/retry), testing scenarios with TestDispatcher, or performance optimization questions.

Here's a concrete example of structured concurrency with proper lifecycle management:

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()
    }
}

Integrations

The skill covers Android ViewModel patterns with StateFlow and MutableStateFlow for reactive UI state management, and Flow-based patterns for location services and data synchronization. Repository patterns integrate with network API services, local database operations, and settings/preference repositories using combine and flatMapLatest operators.

Who it's for

This skill serves Android developers building reactive UIs with ViewModels and StateFlow, backend Kotlin developers implementing async service layers, mobile engineers managing location updates or real-time data streams, and teams requiring robust error handling with supervised jobs and retry logic. It's valuable for developers optimizing coroutine performance with channelFlow, shareIn, stateIn, and backpressure strategies, as well as engineers writing coroutine tests with TestDispatcher.

FAQ

Common questions

Discussion

Questions & comments ยท 0

Sign In Sign in to leave a comment.