一键导入
coroutines-patterns
Kotlin Coroutines and Flow patterns for structured concurrency, error handling, and async operations.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Kotlin Coroutines and Flow patterns for structured concurrency, error handling, and async operations.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Core Android development patterns for Kotlin, including coroutines, lifecycle management, and functional programming idioms.
Apple Combine framework for reactive programming. Publishers, subscribers, operators, and error handling.
Pattern extraction and skill generation for mobile development sessions. Automatically learns from your coding patterns.
Instinct-based learning system with confidence scoring for mobile patterns. Automatically extracts and evolves patterns.
Core Data for iOS persistence. Data models, fetch requests, background contexts, and SwiftData migration.
Kotlin Multiplatform expect/actual patterns for platform-specific APIs. Learn to declare shared interfaces with platform implementations.
| name | coroutines-patterns |
| description | Kotlin Coroutines and Flow patterns for structured concurrency, error handling, and async operations. |
Structured concurrency for Kotlin.
// ✅ 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
// 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 }
}
// 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
.filter { it.isActive }
.map { transform(it) }
.distinctUntilChanged()
.debounce(300)
.catch { emit(fallback) }
.collect { process(it) }
// 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() }
}
// 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.