我们知道StateFlow和SharedFlow很火。
StateFlow is a hot flow—it remains in memory as long as the flow is collected or while any other references to it exist from a garbage collection root.
SharedFlow is a hot flow that emits values to all consumers that collect from it.
流动本身是冷的。
Flows are cold streams similar to sequences...
我有一个问题,但我找不到直接的答案。 LiveData 是热的还是冷的?
最佳答案
LiveData 不能直接与 Flow 相提并论。它与 StateFlow 最直接的可比性。
在 LiveData 对象上调用 setData
的代码总是会更改 LiveData 的状态,无论其上是否有任何观察者。在这方面,它很热 - 观察员的存在根本不重要。状态总是在变化,并且该状态会广播给所有观察者。
此外,LiveData 上的观察者总是会从中获取当前状态值 - 他们不会等待(或暂停)任何值变得可用。 LiveData 必须始终有一个值。在将任何其他数据放入其中之前,观察者将获得该值。
在这些方面,基于与 StateFlow 类似的使用模式,LiveData 很可能被认为是“热门”。
关于android - LiveData 是热的还是冷的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68356910/