SingleLiveData:解决LiveData『数据倒灌』的问题(方案二)

在上一篇文章中(简单粗暴解决LiveData『数据倒灌』的问题),已经解释了什么叫“数据倒灌”,并给出了解决方案,这里再给出另一种解决方案(参考google的SingleLiveEvent)。

方案思路:

1、针对每一个Observer,都设置一个对应的AtomicBoolean值,LiveData执行setValue时置为true,执行onChanged后置为false,确保一个value只分发一次。

完整代码 :

public class SingleLiveData<T> extends MutableLiveData<T> {

    private final HashMap<Observer<? super T>, AtomicBoolean> mPendingMap = new HashMap<>();

    @MainThread
    @Override
    public void observe(@NonNull LifecycleOwner owner, @NonNull final Observer<? super T> observer) {
        Lifecycle lifecycle = owner.getLifecycle();
        if (lifecycle.getCurrentState() == Lifecycle.State.DESTROYED) {
            return;
        }
        mPendingMap.put(observer, new AtomicBoolean(false));
        lifecycle.addObserver((LifecycleEventObserver) (source, event) -> {
            if (event == Lifecycle.Event.ON_DESTROY) {
                mPendingMap.remove(observer);
            }
        });
        super.observe(owner, t -> {
            AtomicBoolean pending = mPendingMap.get(observer);
            if (pending != null && pending.compareAndSet(true, false)) {
                observer.onChanged(t);
            }
        });
    }

    @MainThread
    @Override
    public void observeForever(@NonNull Observer<? super T> observer) {
        mPendingMap.put(observer, new AtomicBoolean(false));
        super.observeForever(observer);
    }

    @MainThread
    @Override
    public void removeObserver(@NonNull Observer<? super T> observer) {
        mPendingMap.remove(observer);
        super.removeObserver(observer);
    }

    @MainThread
    @Override
    public void removeObservers(@NonNull LifecycleOwner owner) {
        mPendingMap.clear();
        super.removeObservers(owner);
    }

    @MainThread
    @Override
    public void setValue(@Nullable T t) {
        for (AtomicBoolean value : mPendingMap.values()) {
            value.set(true);
        }
        super.setValue(t);
    }
}

你可能感兴趣的:(问题记录,数据倒灌,SingleLiveEvent,LiveData,SingleLiveData,MutableLiveData)