帮助开发者掌握 Android 与 KMP 中 Kotlin 协程和 Flow 的核心模式与测试方法
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "kotlin-coroutines-flows" 技能: 1. 下载 https://raw.githubusercontent.com/affaan-m/ECC/main/docs/ja-JP/skills/kotlin-coroutines-flows/SKILL.md 2. 保存为 ~/.claude/skills/kotlin-coroutines-flows/SKILL.md 3. 装好后重载技能,告诉我可以用了
请为一个 Android ViewModel 设计 Kotlin 协程方案:需要并行加载用户资料、消息列表和推荐内容;要求使用结构化并发,支持取消、异常隔离,并说明 supervisorScope 与 coroutineScope 的适用场景,给出示例代码。
一套包含结构化并发说明、作用域选择建议、异常处理策略和可运行示例代码的实现方案。
我有一个搜索框输入流,需要用 Kotlin Flow 实现防抖、去重、切换最新请求,并把结果暴露给 UI。请使用 debounce、distinctUntilChanged、flatMapLatest 和 stateIn,给出 Android 或 KMP 场景下的完整示例。
一份展示 Flow 操作符组合、StateFlow 状态管理和 UI 层接入方式的完整代码示例。
请演示如何测试使用 Kotlin 协程与 Flow 的仓库层:需要覆盖成功、失败、重试和取消场景,使用 kotlinx.coroutines.test,并说明如何测试 StateFlow 发射顺序。
一套包含测试工具用法、关键断言、错误场景覆盖和状态流验证方法的测试示例。
Android および Kotlin Multiplatform プロジェクトにおける構造化並行性、Flow ベースのリアクティブストリーム、コルーチンテストのパターン。
Application
└── viewModelScope (ViewModel)
└── coroutineScope { } (構造化された子)
├── async { } (並行タスク)
└── async { } (並行タスク)
常に構造化並行性を使用してください — GlobalScope は絶対に使わない:
// NG
GlobalScope.launch { fetchData() }
// OK — ViewModel ライフサイクルにスコープ
viewModelScope.launch { fetchData() }
// OK — コンポーザブルライフサイクルにスコープ
LaunchedEffect(key) { fetchData() }
並列作業には coroutineScope + async を使用:
suspend fun loadDashboard(): Dashboard = coroutineScope {
val items = async { itemRepository.getRecent() }
val stats = async { statsRepository.getToday() }
val profile = async { userRepository.getCurrent() }
Dashboard(
items = items.await(),
stats = stats.await(),
profile = profile.await()
)
}
子の失敗が兄弟をキャンセルしてはならない場合は supervisorScope を使用:
suspend fun syncAll() = supervisorScope {
launch { syncItems() } // ここでの失敗は syncStats をキャンセルしない
launch { syncStats() }
launch { syncSettings() }
}
fun observeItems(): Flow<List<Item>> = flow {
// データベースが変更されるたびに再エミット
itemDao.observeAll()
.map { entities -> entities.map { it.toDomain() } }
.collect { emit(it) }
}
class DashboardViewModel(
observeProgress: ObserveUserProgressUseCase
) : ViewModel() {
val progress: StateFlow<UserProgress> = observeProgress()
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = UserProgress.EMPTY
)
}
WhileSubscribed(5_000) は最後のサブスクライバーが離れてから 5 秒間アップストリームをアクティブに保ちます — 設定変更を再起動なしに生き延びます。
val uiState: StateFlow<HomeState> = combine(
itemRepository.observeItems(),
settingsRepository.observeTheme(),
userRepository.observeProfile()
) { items, theme, profile ->
HomeState(items = items, theme = theme, profile = profile)
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), HomeState())
// 検索入力のデバウンス
searchQuery
.debounce(300)
.distinctUntilChanged()
.flatMapLatest { query -> repository.search(query) }
.catch { emit(emptyList()) }
.collect { results -> _state.update { it.copy(results = results) } }
// 指数バックオフでリトライ
fun fetchWithRetry(): Flow<Data> = flow { emit(api.fetch()) }
.retryWhen { cause, attempt ->
if (cause is IOException && attempt < 3) {
delay(1000L * (1 shl attempt.toInt()))
true
} else {
false
}
}
class ItemListViewModel : ViewModel() {
private val _effects = MutableSharedFlow<Effect>()
val effects: SharedFlow<Effect> = _effects.asSharedFlow()
sealed interface Effect {
data class ShowSnackbar(val message: String) : Effect
data class NavigateTo(val route: String) : Effect
}
private fun deleteItem(id: String) {
viewModelScope.launch {
repository.delete(id)
_effects.emit(Effect.ShowSnackbar("Item deleted"))
}
}
}
// コンポーザブルでコレクト
LaunchedEffect(Unit) {
viewModel.effects.collect { effect ->
when (effect) {
is Effect.ShowSnackbar -> snackbarHostState.showSnackbar(effect.message)
is Effect.NavigateTo -> navController.navigate(effect.route)
}
}
}
// CPU 集約型作業
withContext(Dispatchers.Default) { parseJson(largePayload) }
// IO バウンド作業
withContext(Dispatchers.IO) { database.query() }
…
通过双评审智能体对结果进行对抗式校验,提升输出发布前的可靠性
提供 Kotlin 测试模式与 TDD 实践,涵盖 MockK、协程测试、性质测试和覆盖率分析。