public class ProtectedUnPeekLiveData<T>
extends androidx.lifecycle.LiveData<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 19/9/23
| 限定符和类型 | 字段和说明 |
|---|---|
protected boolean |
isAllowNullValue |
| 构造器和说明 |
|---|
ProtectedUnPeekLiveData() |
| 限定符和类型 | 方法和说明 |
|---|---|
protected void |
clear() |
void |
observeInActivity(androidx.appcompat.app.AppCompatActivity activity,
androidx.lifecycle.Observer<? super T> observer) |
void |
observeInFragment(androidx.fragment.app.Fragment fragment,
androidx.lifecycle.Observer<? super T> observer) |
protected 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
|
public void observeInActivity(@NonNull
androidx.appcompat.app.AppCompatActivity activity,
@NonNull
androidx.lifecycle.Observer<? super T> observer)
public void observeInFragment(@NonNull
androidx.fragment.app.Fragment fragment,
@NonNull
androidx.lifecycle.Observer<? super T> observer)
protected void setValue(T value)
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
setValue 在类中 androidx.lifecycle.LiveData<T>value - protected void clear()