coroutine stateflow.collect {}未触发

发布于 2025-02-04 03:49:32 字数 1375 浏览 4 评论 0原文

我看到一些奇怪的行为。我在我的viewModel中有一个简单的stateflow< boolean>,该未在片段中收集。定义:

private val _primaryButtonClicked = MutableStateFlow(false)
val primaryButtonClicked: StateFlow<Boolean> = _primaryButtonClicked

这是我设置值的地方:

fun primaryButtonClick() {
    _primaryButtonClicked.value = true
}

这是我收集的地方。

     repeatOnOwnerLifecycle {
        launch(dispatchProvider.io()) {
            freeSimPurchaseFragmentViewModel.primaryButtonClicked.collect {
                if (it) {
                    autoCompletePlacesStateFlowModel.validateErrors()
                    formValidated = autoCompletePlacesStateFlowModel.validateAddress()
                    if (formValidated) {
                        freeSimPurchaseFragmentViewModel
                            .sumbitForm(autoCompletePlacesStateFlowModel.getStateFlowCopy())
                    }
                }
            }
        }
     }

retooneNownerLifecycle

inline fun Fragment.repeatOnOwnerLifecycle(
    state: Lifecycle.State = Lifecycle.State.RESUMED,
    crossinline block: suspend CoroutineScope.() -> Unit
) {
    viewLifecycleOwner.lifecycleScope.launch {
     repeatOnLifecycle(state) {
        block()
   }
}

我在做什么错?收藏家永远不会开火。

I'm seeing some odd behavior. I have a simple StateFlow<Boolean> in my ViewModel that is not being collected in the fragment. Definition:

private val _primaryButtonClicked = MutableStateFlow(false)
val primaryButtonClicked: StateFlow<Boolean> = _primaryButtonClicked

and here is where I set the value:

fun primaryButtonClick() {
    _primaryButtonClicked.value = true
}

Here is where I'm collecting it.

     repeatOnOwnerLifecycle {
        launch(dispatchProvider.io()) {
            freeSimPurchaseFragmentViewModel.primaryButtonClicked.collect {
                if (it) {
                    autoCompletePlacesStateFlowModel.validateErrors()
                    formValidated = autoCompletePlacesStateFlowModel.validateAddress()
                    if (formValidated) {
                        freeSimPurchaseFragmentViewModel
                            .sumbitForm(autoCompletePlacesStateFlowModel.getStateFlowCopy())
                    }
                }
            }
        }
     }

repeatOnOwnerLifecycle:

inline fun Fragment.repeatOnOwnerLifecycle(
    state: Lifecycle.State = Lifecycle.State.RESUMED,
    crossinline block: suspend CoroutineScope.() -> Unit
) {
    viewLifecycleOwner.lifecycleScope.launch {
     repeatOnLifecycle(state) {
        block()
   }
}

What am I doing wrong? The collector never fires.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是你 2025-02-11 03:49:32

这有意义吗?

val primaryButtonClicked: StateFlow<Boolean> = _primaryButtonClicked.asStateFlow() 

另外,我无法理解内联函数部分,因为在引擎盖下,您写了类似的东西,

viewLifecycleOwner.lifecycleScope.launch {
   viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
      launch(dispatchProvider.io()) {
        freeSimPurchaseFragmentViewModel.primaryButtonClicked.collect {
            if (it) {
                autoCompletePlacesStateFlowModel.validateErrors()
                formValidated = autoCompletePlacesStateFlowModel.validateAddress()
                if (formValidated) {
                    freeSimPurchaseFragmentViewModel
                        .sumbitForm(autoCompletePlacesStateFlowModel.getStateFlowCopy())
                }
            }
        }
     }
   }
}

为什么您在另一个coroutine中启动一个Coroutine并从IO调度员那里收集流程?您需要从主调度器收集值。

Does this make sense?

val primaryButtonClicked: StateFlow<Boolean> = _primaryButtonClicked.asStateFlow() 

Also I couldn't understand the inline function part, because under the hood seems you wrote something like this

viewLifecycleOwner.lifecycleScope.launch {
   viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
      launch(dispatchProvider.io()) {
        freeSimPurchaseFragmentViewModel.primaryButtonClicked.collect {
            if (it) {
                autoCompletePlacesStateFlowModel.validateErrors()
                formValidated = autoCompletePlacesStateFlowModel.validateAddress()
                if (formValidated) {
                    freeSimPurchaseFragmentViewModel
                        .sumbitForm(autoCompletePlacesStateFlowModel.getStateFlowCopy())
                }
            }
        }
     }
   }
}

Why are you launching one coroutine in another and collect the flow from IO dispatcher? You need to collect the values from the main dispatcher.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文