根据LiveData
documentation ,其中一项功能旨在使用可观察模式通知数据更改。
(除此之外,LiveData 还提供了许多其他有前途的功能)
另一方面,Observable data object
基于其 documentation能够通知其他人其数据的变化。
因此,这两个功能似乎是相同的。
问题是:
用LiveData
不是更好吗?它的其他功能?
最佳答案
根据 LiveData 文档:
LiveData is an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services. This awareness ensures LiveData only updates app component observers that are in an active lifecycle state.
所以它本质上是一个 生命周期感知 observable,现在它是一个优势,不是吗?
但是,对于仅使用
RxJava
确实没有任何限制。或 LiveData
,两者可以一起使用。实际上,当没有可用的生命周期(如 )时,最好使用 RxJava。存储库 在数据层并保留
LiveData
在生命周期很重要的层中,例如 介绍 层。
关于android-databinding - LiveData 与 Observable 数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53572489/