LiveData源码阅读

本文解析了Android LiveData中的setValue与postValue操作的区别,重点在于它们的执行时机、线程控制和值的最终应用。setValue是同步执行,postValue则异步并将值暂存,确保在主线程更新。
摘要由CSDN通过智能技术生成

一、 一次setValue的更新流程

setValue 源码

    @MainThread
    protected void setValue(T value) {
        assertMainThread("setValue");
        mVersion++;
        mData = value;
        dispatchingValue(null);
    }
1、setValue会同步完成Value的更新,只能在主线程中调用
assertMainThread("setValue")
此方法会检查SetValue是不是在主线程中执行,若不是在主线程中,会直接报错
内部具体判断在 DefaultTaskExeutor  类的 isMainThread 中
    @Override
    public boolean isMainThread() {
        return Looper.getMainLooper().getThread() == Thread.currentThread();
    }
2、数据分发
dispatchingValue(null); //顾名思义,这个函数就是数据分发类
   @SuppressWarnings("WeakerAccess") /* synthetic access */
    void dispatchingValue(@Nullable ObserverWrapper initiator) {
        if (mDispatchingValue) {
            mDispatchInvalidated = true;
            return;
        }
        mDispatchingValue = true;
        do {
            mDispatchInvalidated = false;
            if (initiator != null) {
                considerNotify(initiator);
                initiator = null;
            } else {
                for (Iterator<Map.Entry<Observer<? super T>, ObserverWrapper>> iterator =
                        mObservers.iteratorWithAdditions(); iterator.hasNext(); ) {
                    considerNotify(iterator.next().getValue());
                    if (mDispatchInvalidated) {
                        break;
                    }
                }
            }
        } while (mDispatchInvalidated);
        mDispatchingValue = false;
    }

抛去一些并发相关和情况的bool参数判断,实际上具体的分发操作就是

considerNotify(iterator.next().getValue()); 

函数来完成的

3、通知观察者
    @SuppressWarnings("unchecked")
    private void considerNotify(ObserverWrapper observer) {
        if (!observer.mActive) {
            return;
        }
        // Check latest state b4 dispatch. Maybe it changed state but we didn't get the event yet.
        //
        // we still first check observer.active to keep it as the entrance for events. So even if
        // the observer moved to an active state, if we've not received that event, we better not
        // notify for a more predictable notification order.
        if (!observer.shouldBeActive()) {
            observer.activeStateChanged(false);
            return;
        }
        if (observer.mLastVersion >= mVersion) {
            return;
        }
        observer.mLastVersion = mVersion;
        observer.mObserver.onChanged((T) mData);
    }
前面的代码都是在判断 观察者的生命周期 和 版本号
最后一句 observer.mObserver.onChanged((T) mData); 就是通知观察者的函数

二、postValue跟setValue

postValue的源码
    /**
     * Posts a task to a main thread to set the given value. So if you have a following code
     * executed in the main thread:
     * <pre class="prettyprint">
     * liveData.postValue("a");
     * liveData.setValue("b");
     * </pre>
     * The value "b" would be set at first and later the main thread would override it with
     * the value "a".
     * <p>
     * If you called this method multiple times before a main thread executed a posted task, only
     * the last value would be dispatched.
     *
     * @param value The new value
     */
    protected void postValue(T value) {
        boolean postTask;
        synchronized (mDataLock) {
            postTask = mPendingData == NOT_SET;
            mPendingData = value;
        }
        if (!postTask) {
            return;
        }
        ArchTaskExecutor.getInstance().postToMainThread(mPostValueRunnable);
    }
1、注解1
 liveData.postValue("a");
 liveData.setValue("b");
 The value "b" would be set at first and later the main thread would override it with
 the value "a".

先执行 post a ,后执行 set b。但是 b 会被先复制给 value,然后 a 会把 b覆盖掉。
这是因为 使用 post 方法 去更新value的值, 会调用
ArchTaskExecutor.getInstance().postToMainThread(mPostValueRunnable);
,把更新的操作post到 messageQueue里面去,等消息队列去 异步执行。而由于setValue函数是同步执行的,所以先调用postValue反而会在setValue之后更新value。
2、注解2
If you called this method multiple times before a main thread executed a posted task, only the last value would be dispatched.

在主线程执行了第一个已经被post的task(就是mPostValueRunnable) 之前,如果多次调用postValue方法,只有最后一个post的Value会被分发。

也就是多次调用postValue,只有最后一次post会生效。

首先看 mPostValueRunnable 这个runnable干了什么
private final Runnable mPostValueRunnable = new Runnable() {
        @SuppressWarnings("unchecked")
        @Override
        public void run() {
            Object newValue;
            synchronized (mDataLock) {
                newValue = mPendingData;
                mPendingData = NOT_SET;
            }
            setValue((T) newValue);
        }
    };
最后还是由setValue来完成更新和分发value的操作。
可以看到,newValue其实也就是mPendingData,而我们在每次调用postValue的时候,都会给mPendingData赋值。
所以多次调用postValue之后,mPendingData的值就是最后一次PostValue的值,而mPostValueRunnable此时还在messageQueue里面,还没有来得及执行,mPostValueRunnable里面的newValue最后都会变成最后一次的post的Value。
3、总结

由此来看,postValue跟setValue的区别就是

1、postValue可以在子线程中使用,而setValue不行。

2、postValue最后也还是使用setValue来完成更新和分发操作的,但是会把setValue加入到消息队列中稍后执行,setValue会立即执行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值