LiveData 是热的还是冷的?

Is LiveData hot or cold?

我们知道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 很可能被认为是“热门”。