kotlin-coroutines-flows
Kotlin coroutines and Flow patterns in Android and KMP—structured concurrency, Flow operators, StateFlow, error handling, and testing.
Machine translationRead the original Chinese text
Kotlin协程与Flow在Android和KMP中的模式——结构化并发、Flow操作符、StateFlow、错误处理和测试。
Target: Mobile development. Automatically collected · unverified.
Excerpt from the author’s documentation
Extracted from a fixed version of SKILL.md in its original language. This is the author’s documentation; installation, compatibility and runtime results have not been verified by this site.
Read the original Chinese text
Topics in the source
- Kotlin 协程与 Flow
- 何时启用
- 结构化并发
- 作用域层级
- 并行分解
- SupervisorScope
- Flow 模式
- Cold Flow —— 一次性操作到流的转换
适用于 Android 和 Kotlin 多平台项目的结构化并发模式、基于 Flow 的响应式流以及协程测试。 使用 Kotlin 协程编写异步代码 使用 Flow、StateFlow 或 SharedFlow 实现响应式数据 处理并发操作(并行加载、防抖、重试) 测试协程和 Flow 管理协程作用域与取消 始终使用结构化并发——绝不使用 GlobalScope: 使用 coroutineScope + async 处理并行工作: 当子协程失败不应取消同级协程时,使用 supervisorScope: WhileSubscribed(5000) 会在最后一个订阅者离开后,保持上游活动 5 秒——可在配置更改时存活而无需重启。 在 KMP 中,使用 Dispatchers.Default 和 Dispatchers.Main(在所有平台上可用)。Dispatchers.IO 仅适用于 JVM/Android——在其他平台上使用 Dispatchers.Default 或通过依赖注入提供。 长时间运行的循环必须检查取消状态: 使用 GlobalScope——会导致协程泄漏,且无法结构化取消 在没有作用域的情况下于 init {} 中收集 Flow——应使用 viewModelScope.launch 将 MutableStateFlow 与可变集合一起使用——始终使用不可变副本:state.update { it.copy(list = it.list + newItem) } 捕获 CancellationException——应让其传播以实现正确的取消 使用 flowOn(Dispatchers.Main) 进行收集——收集调度器是调用方的调度器 在 @Composable 中创建 Flow 而不使用 remember——每次重组都会重新创建 Flow 关于 Flow 在 UI 层的消费,请参阅技能:compose-multiplatform-patterns。 关于协程在各层中的适用位置,请参阅技能:android-clean-architecture。
- Source repository
- affaan-m/ECC
- Source file
- docs/zh-CN/skills/kotlin-coroutines-flows/SKILL.md
- Fixed revision
- d2ef2680594f9a17526a6e1a2e18b3d242c5d17b
Requirements
- Only the author’s Skill metadata is included. Read the full instructions to check dependencies, licensing and conditions of use.
A small first task
先阅读原作者说明,再决定是否在自己的 AI 工具中使用。
Instructions for your AI tool
- Claude Desktop: resource-specific guide not available yet
- Claude Code: resource-specific guide not available yet
- Cursor: resource-specific guide not available yet
- Codex: resource-specific guide not available yet
Author documentation is not live verification by this site. Without evidence, we do not mark a resource as compatible, safe or free.
Sources and evidence
This record was collected automatically. Its documentation, suitability and runtime behavior have not been manually verified.
Author: affaan-m/ECC · original repository metadata. Documentation review: Not reviewed. Last source check: 2026-11-09T15:01:42.397Z.
Categories are inferred from names, descriptions and paths. They do not establish compatibility.
License and attribution
MIT. Third-party content and copyright remain with their authors. Refer to the original license and terms.
Original license ↗Synchronization, copying and user-reported success do not mark a resource as runtime-tested.
Limitations and notes
- Collecting metadata does not mean this site has installed, adapted, runtime-tested or security-reviewed the resource.