(三) Jetpack LiveData添加Observer立即调用onChanged方法流程

LiveData中添加Observer,Observer.onChanged()为何被立即调用?

  1. 调用LiveData.observe(owner,observer)
public abstract class LiveData<T> {
    @MainThread
    public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
        ...
        // 将observer添加到LifecycleBoundObserver对象中.
        LifecycleBoundObserver wrapper = new LifecycleBoundObserver(owner, observer);
        ObserverWrapper existing = mObservers.putIfAbsent(observer, wrapper);
        ...
        // 将LifecycleBoundObserver对象添加到LifecycleRegistry中
        // 想了解这里可以看Lifecycle那篇文章
        owner.getLifecycle().addObserver(wrapper);
    }
}
  1. LifecycleRegistry.addObserver(wrapper)
    这里将LifecycleBoundObserver对象与Activity或者Fragment的声明周期关联起来.
public class LifecycleRegistry extends Lifecycle {
    @Override
    public void addObserver(@NonNull LifecycleObserver observer) {
        State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED;
        // LifecycleBoundObserver对象被装入了ObserverWithState对象中, 当前ObserverWithState状态为DESTROYED.
        ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);
        ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);
        ...
        // ObserverWithState当前的状态与Activity或者Fragment组件状态比较,如果小与组件状态,那么就要对ObserverWithState状态进行提升了.
        while ((statefulObserver.mState.compareTo(targetState) < 0 && mObserverMap.contains(observer))) {
            ...
            // 这里开始分发事件,分发事件的同时ObserverWithState的状态也会改变.
            statefulObserver.dispatchEvent(lifecycleOwner, upEvent(statefulObserver.mState));
        }
        if (!isReentrance) {
            // 这里是重入,不会走这.
            sync();
        }
    }
}
  1. ObserverWithState分发事件
public class LifecycleRegistry extends Lifecycle {
    static class ObserverWithState {
        static class ObserverWithState {
            State mState; // DESTROYED状态
            LifecycleEventObserver mLifecycleObserver;// LifecycleBoundObserver类型
            ObserverWithState(LifecycleObserver observer, State initialState) {
                mLifecycleObserver = Lifecycling.lifecycleEventObserver(observer);
                mState = initialState;
            }
            void dispatchEvent(LifecycleOwner owner, Event event) {
                // 忽略状态的改变, 只看如何分发事件的.
                State newState = getStateAfter(event);
                mState = min(mState, newState);
                // 这里调用的是LifecycleBoundObserver.onStateChanged()
                mLifecycleObserver.onStateChanged(owner, event);
                mState = newState;
            }
        }
    }
}
  1. 事件分发到了LifecycleBoundObserver对象中
public abstract class LiveData<T> {
    class LifecycleBoundObserver extends ObserverWrapper implements LifecycleEventObserver {
        @Override
        public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
             // 其实event到此就没有用了,下面做的主要是目的是回调观察者的onChanged()方法.
             ...
            // shouldBeActive(): 该方法用来判断当前是否是活跃状态,如果组件的状态大于或者等于STARTED,那么当前观察者就是活跃的.
            // 向下看
            activeStateChanged(shouldBeActive());
        }
    }
    // 这是LifecycleBoundObserver的父类
    private abstract class ObserverWrapper {
        void activeStateChanged(boolean newActive) {
            ...
            // 观察者如果是活跃的,就继续向下调用.
            if (mActive) {
                dispatchingValue(this);
            }
        }
    }  
    // 该方法属于LiveData
    void dispatchingValue(@Nullable ObserverWrapper initiator) {
        ...
        do {
            mDispatchInvalidated = false;
            if (initiator != null) {
                // 走这里
                considerNotify(initiator);
                initiator = null;
            } else {
                // 这里是用setValue()方法触发的.
                ...
            }
        } while (mDispatchInvalidated);
    }
    // 该方法属于LiveData
    private void considerNotify(ObserverWrapper observer) {
        ...
        // mVersion:它属于LiveData,初始值为0.每次setValue()都会让它+1.
        // mLastVersion: 它属于Observer,每当创建一个观察者,该值的初始值为-1.
        if (observer.mLastVersion >= mVersion) {
            return;
        }
        observer.mLastVersion = mVersion;
        // 这里就正式的调用到了LifecycleBoundObserver当中的观察者的onChanged()方法.
        observer.mObserver.onChanged((T) mData);
    }
}

你可能感兴趣的:(Jetpack)