gpt4 book ai didi

android - SharedFlow 和 StateFlow 的主要区别

转载 作者:行者123 更新时间:2023-12-04 23:37:18 32 4
gpt4 key购买 nike

有什么区别?共享流 状态流 ?
以及如何在 中使用这些MVI 建筑学?使用简单 更好吗?流量 还是这些状态和事件?

最佳答案

Flow 很冷!,这意味着它仅在收集数据时才发出数据。
Flow 也不能保存数据,将其视为水在其中流动的管道,flow 中的数据仅流动,而不是存储(没有 .value 函数)。
与 flow 不同,stateflow 和 sharedflow 是热流,这意味着即使没有收集器,它们也会发出数据。此外,如果有多个收集器,将为每个收集器运行一个新流程,彼此完全独立。
SharedFlow 和 StateFlow 是允许在多个收集器之间共享自身的流,因此只有一个流有效地为所有同时的收集器运行。如果你定义了一个访问数据库的 SharedFlow,它被多个收集器收集,那么数据库访问只会运行一次,结果数据将共享给所有收集器。
SharedFlow 和 StateFlow 有什么区别?
状态流

Stateflow takes an initial value through constructor and emits it immediatelywhen someone starts collecting.Stateflow is identical to LiveData.LiveData automatically unregisters the consumer when the view goes to the STOPPEDstate. When collecting a StateFlow this is not handled automatically , you canuse repeatOnLifeCyCle scope if you want to unregister the consumer on STOPPEDstate.If you want current state use stateflow(.value).



共享流

StateFlow only emits last known value , whereas sharedflow can configure how manyprevious values to be emitted.If you want emitting and collecting repeated values , use sharedflow.

关于android - SharedFlow 和 StateFlow 的主要区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66162586/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com