StateFlow 收集在一个协程中

问题描述

我尝试在一个协程中初始化三个收集,但只在第一个工作。 只有当我在不同的协程中设置收集它的工作。为什么?

  lifecycleScope.launch {
            launch {
                homeviewmodel.dateStateFlow().collect { date ->
                    date?.let { calendar.text = date.toStringForView() }
                }
            }
            launch {
                homeviewmodel.toStateFlow().collect { to ->
                    to?.let { cityTo.text = to.name }
                }
            }
            launch {
                homeviewmodel.fromStateFlow().collect { from ->
                    from?.let { cityFrom.text = from.name }
                }
            }
        }

解决方法

StateFlow 永远不会完成,因此收集它是一个无限的操作。这在 the documentation of StateFlow 中有解释。协程是连续的,因此如果您在 StateFlow 上调用 collect,则协程中该调用之后的任何代码都不会被访问。

由于收集 StateFlows 和 SharedFlows 来更新 UI 很常见,所以我使用这样的辅助函数来使它更简洁:

fun <T> LifecycleOwner.collectWhenStarted(flow: Flow<T>,firstTimeDelay: Long = 0L,action: suspend (value: T) -> Unit) {
    lifecycleScope.launch {
        delay(firstTimeDelay)
        lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
            flow.collect(action)
        }
    }
}

// Usage:

collectWhenStarted(homeViewModel.dateStateFlow()) { date ->
    date?.let { calendar.text = date.toStringForView() }
}