LiveData basic introduction:

See the official Google documentation

A constructor

LiveData has two constructors, parameter-free and parameter-free

public LiveData(T value) {
    mData = value;
    mVersion = START_VERSION + 1;
}

public LiveData(a) {
    mData = NOT_SET;
    mVersion = START_VERSION;
}
Copy the code

You can see that the liveData constructor assigns values to the mData and mVersion variables. If you look at where the values are used and assigned, you can see that mData is the data source that needs to be sent

observer.mObserver.onChanged((T) mData);
Copy the code

MVersion is used to determine whether the current mData has been sent

if (observer.mLastVersion >= mVersion) {
    return;
}
observer.mLastVersion = mVersion;
Copy the code

observe

So with that constructor out of the way let’s look at the observe method, which is the core LiveData method and the observe method takes two parameters, a LifecycleOwner and an Observer LifecycleOwner to bind the lifecycle, An Observer is a data and lifecycle change Observer

@MainThread
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
    // Check whether it is the main thread
    assertMainThread("observe");
    if (owner.getLifecycle().getCurrentState() == DESTROYED) {
        // ignore
        return;
    }
    // Build a LifecycleBoundObserver to observe the lifecycle
    LifecycleBoundObserver wrapper = new LifecycleBoundObserver(owner, observer);
    // Query whether the observer has been added
    ObserverWrapper existing = mObservers.putIfAbsent(observer, wrapper);
    if(existing ! =null && !existing.isAttachedTo(owner)) {
        throw new IllegalArgumentException("Cannot add the same observer"
                + " with different lifecycles");
    }
    if(existing ! =null) {
        return;
    }
    // Add an observer to observe lifecycle changes
    owner.getLifecycle().addObserver(wrapper);
}
Copy the code

A simple flow chart is shown below LiveDataNo binding lifecycle is also providedobserveForeverMethods.

setValue

Let’s look again at the setValue method of LiveData

protected void setValue(T value) {
    assertMainThread("setValue");
    mVersion++;
    mData = value;
    dispatchingValue(null);
}
Copy the code

You can see that dispatchingValue(NULL) is called after assignment to mData

void dispatchingValue(@Nullable ObserverWrapper initiator) {
    // Determine if any data is being sent
    if (mDispatchingValue) {
        mDispatchInvalidated = true;
        return;
    }
    mDispatchingValue = true;
    do {
        mDispatchInvalidated = false;
        // The incoming observer is not empty and directly notifies the observer of updates
        if(initiator ! =null) {
            considerNotify(initiator);
            initiator = null;
        } else {
        // Poll all observers
            for (Iterator<Map.Entry<Observer<? super T>, ObserverWrapper>> iterator =
                    mObservers.iteratorWithAdditions(); iterator.hasNext(); ) {
                considerNotify(iterator.next().getValue());
                if (mDispatchInvalidated) {
                    break; }}}}while (mDispatchInvalidated);
    // Data update completed
    mDispatchingValue = false;
}
Copy the code