public class UnPeekLiveData<T> extends ProtectedUnPeekLiveData<T>
https://xiaozhuanlan.com/topic/6719328450
本类参考了官方 SingleEventLive 的非入侵设计, 以及小伙伴 Flywith24 在 wrapperLiveData 中通过 ViewModelStore 来唯一确定订阅者的思路,
TODO:在当前最新版中,我们透过对 ViewModelStore 的内存地址的遍历, 来确保: 1.一条消息能被多个观察者消费 2.消息被所有观察者消费完毕后才开始阻止倒灌 3.可以通过 clear 方法手动将消息从内存中移除 4.让非入侵设计成为可能,遵循开闭原则
TODO:增加一层 ProtectedUnPeekLiveData, 用于限制从 Activity/Fragment 篡改来自 "数据层" 的数据,数据层的数据务必通过 "唯一可信源" 来分发, 如果这样说还不理解,详见: https://xiaozhuanlan.com/topic/0168753249 和 https://xiaozhuanlan.com/topic/6719328450
Create by KunMinX at 2020/7/21
| 限定符和类型 | 类和说明 |
|---|---|
static class |
UnPeekLiveData.Builder<T> |
isAllowNullValue| 构造器和说明 |
|---|
UnPeekLiveData() |
| 限定符和类型 | 方法和说明 |
|---|---|
void |
observe(androidx.lifecycle.LifecycleOwner owner,
androidx.lifecycle.Observer<? super T> observer)
已过时。
|
void |
observeForever(androidx.lifecycle.Observer<? super T> observer)
已过时。
|
void |
postValue(T value) |
void |
setValue(T value)
重写的 setValue 方法,默认不接收 null
可通过 Builder 配置允许接收
可通过 Builder 配置消息延时清理的时间
override setValue, do not receive null by default
You can configure to allow receiving through Builder
And also, You can configure the delay time of message clearing through Builder
|
clear, observeInActivity, observeInFragmentpublic void setValue(T value)
ProtectedUnPeekLiveDataoverride setValue, do not receive null by default You can configure to allow receiving through Builder And also, You can configure the delay time of message clearing through Builder
setValue 在类中 ProtectedUnPeekLiveData<T>@Deprecated
public void observe(@NonNull
androidx.lifecycle.LifecycleOwner owner,
@NonNull
androidx.lifecycle.Observer<? super T> observer)
2020.10.15 背景缘由: UnPeekLiveData 通过 ViewModelStore 来在各种场景下(如旋屏后)确定订阅者的唯一性和消息的消费状况, 因而在 Activity 和 fragment 对 LifecycleOwner 的使用存在差异的现状下, 我们采取注入局部变量的方式,来获取 store 和 owner。
observe 在类中 androidx.lifecycle.LiveData<T>owner - observer - @Deprecated
public void observeForever(@NonNull
androidx.lifecycle.Observer<? super T> observer)
2020.8.1 背景缘由: UnPeekLiveData 主要用于表现层的 页面转场 和 页面间通信 场景下的非粘性消息分发, 出于生命周期安全等因素的考虑,不建议使用 observeForever 方法,
对于数据层的工作,如有需要,可结合实际场景使用 RxJava 或 kotlin flow。
observeForever 在类中 androidx.lifecycle.LiveData<T>observer -