美文网首页
JetPack-LiveData源码解析

JetPack-LiveData源码解析

作者: _Once1 | 来源:发表于2020-07-25 23:21 被阅读0次

LiveData原理分析

使用方法

这里借助谷歌官方文档来简单说明LiveData的用法:

    class NameViewModel : ViewModel() {

        // Create a LiveData with a String
        val currentName: MutableLiveData<String> by lazy {
            MutableLiveData<String>()
        }

        // Rest of the ViewModel...
    }
    

创建一个LiveData对象

class NameActivity : AppCompatActivity() {

    // Use the 'by viewModels()' Kotlin property delegate
    // from the activity-ktx artifact
    private val model: NameViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Other code to setup the activity...

        // Create the observer which updates the UI.
        val nameObserver = Observer<String> { newName ->
            // Update the UI, in this case, a TextView.
            nameTextView.text = newName
        }

        // Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
        model.currentName.observe(this, nameObserver)

        button.setOnClickListener {
            val anotherName = "John Doe"
            model.currentName.setValue(anotherName)
        }
    }
}

调用了observe方法后,就已经注册了监听。
后面不需要在onDestroy内部移除observer,这一切都在livedata内部做好了,非常方便。
下面来看它的逻辑是怎样实现的


源码分析

首先,它的内部是通过一个Map存储所有的观察者
private SafeIterableMap<Observer<? super T>, ObserverWrapper> mObservers = new SafeIterableMap<>();
key是observer,value是包装类LifecycleBoundObserver。

当调用observe()方法时,会将其放入mObservers中。

    public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
        assertMainThread("observe");
        if (owner.getLifecycle().getCurrentState() == DESTROYED) {
            // ignore
            return;
        }
        LifecycleBoundObserver wrapper = new LifecycleBoundObserver(owner, observer);
        // 1
        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;
        }
        // 2 
        owner.getLifecycle().addObserver(wrapper);
    }

关键点如上面
注释1,先将二者放入map中,便于后面通知所有的观察者;
注释2处将当前wrapper再添加进LifeCycle的观察者中,以便在页面生命周期变化时,得到通知,这部分后面再具体说明。

通过第一步,观察者已经添加进来了,一共有两部分:

  • 外面添加的用于观察数据变化的observer
  • LiveData自身向LifeCycle添加的生命周期wrapper observer

下面分别看看它们是怎样通知数据变化的。


数据变化的通知

当调用setValue设置数据时,代码如下:

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

主要是dispatchingValue方法,如果参数传入null,则会遍历通知所有的观察者,否则只会通知传入的特定观察者

    void dispatchingValue(@Nullable ObserverWrapper initiator) {
        if (mDispatchingValue) {
            mDispatchInvalidated = true;
            return;
        }
        mDispatchingValue = true;
        do {
            mDispatchInvalidated = false;
            if (initiator != null) {
                // 如果该参数不是null,那么只会通知这个特定的观察者
                considerNotify(initiator);
                initiator = null;
            } else {
                // 否则,循环遍历mObservers,逐个通知
                for (Iterator<Map.Entry<Observer<? super T>, ObserverWrapper>> iterator =
                        mObservers.iteratorWithAdditions(); iterator.hasNext(); ) {
                    considerNotify(iterator.next().getValue());
                    if (mDispatchInvalidated) {
                        break;
                    }
                }
            }
        } while (mDispatchInvalidated);
        mDispatchingValue = false;
    }

这里传入的参数是null,主要看for循环中的considerNotify方法

    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.
        // 为了万无一失,再次检查页面的状态是否已经start或resume
        if (!observer.shouldBeActive()) {
            // 如果不是,则重新刷新LiveData内部生命周期监听的状态
            observer.activeStateChanged(false);
            return;
        }
        if (observer.mLastVersion >= mVersion) {
            return;
        }
        observer.mLastVersion = mVersion;
        //noinspection unchecked
        // 回调onChanged方法,这里就回到我们设置的自定义监听了
        observer.mObserver.onChanged((T) mData);
    }

    @Override
    boolean shouldBeActive() {
        return mOwner.getLifecycle().getCurrentState().isAtLeast(STARTED);
    }

上面的逻辑简单总结如下:
由于在LiveData内部持有了LifeCycle的生命周期监听,在数据发生变化,要通知外部的时候,先检查生命周期的状态,如果已经不是在start或resume,则不会发出通知。否则,就回调onChanged方法


生命周期变化的监听

在调用LiveData.observe()方法时,其内部同时调用了LifeCycle.addObserver()方法,传入的参数是LiveData的内部类LifecycleBoundObserver,它实现了GenericLifecycleObserver接口。

主要看下它的onStateChanged()方法

@Override
boolean shouldBeActive() {
    return mOwner.getLifecycle().getCurrentState().isAtLeast(STARTED);
}

@Override
public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
    if (mOwner.getLifecycle().getCurrentState() == DESTROYED) {
        removeObserver(mObserver);
        return;
    }
    activeStateChanged(shouldBeActive());
}

// 该方法主要在监测到页面销毁时调用,移除自身作为lifeCycle的观察者
void detachObserver() {
    mOwner.getLifecycle().removeObserver(this);
}

可以看到,当观察到页面销毁时,会调用LiveData.removeObserver去移除当前相关联的observer,也就是我们调用LiveDta.observe()时传入的自定义数据观察者,避免了内存泄露产生。

当不是destroy状态,而是start或者resume时,则会去调用activeStateChanged(true),随后,又会调用dispatchingValue(this),上文我们讲过,当传入的参数不是null的时候,只会通知这一条特定的观察者,这时一条完整的通知逻辑就出现了。
这里使得当应用从后台切到前台时,数据始终可以保持是最新的状态

    public void removeObserver(@NonNull final Observer<? super T> observer) {
        assertMainThread("removeObserver");
        // 先删除我们传入的observer
        ObserverWrapper removed = mObservers.remove(observer);
        if (removed == null) {
            return;
        }

        // 再将自身作为lifeCycle观察者的身份移除
        removed.detachObserver();
        removed.activeStateChanged(false);
    }

关键点都在代码中注释出来了。
直到将两个观察者对象都完全移除,该观察者的责任就走到了尽头。


总结

  • LiveData是黏性监听的。在首次设置监听时,会遍历所有的观察者,做一次通知
  • LiveData通过维护2个观察者,实现了数据+生命周期的监听。一个是我们传入的数据Observer,另一个是其自身向LifeCycle设置的生命周期监听
  • LiveData只会去给处于前台(STARTED和RESUMED) 的页面发送数据变化通知
  • 不会发生内存泄露:类似的,在监测到页面Destroy时,会同时移除上面两个监听,避免内存泄露

相关文章

网友评论

      本文标题:JetPack-LiveData源码解析

      本文链接:https://www.haomeiwen.com/subject/bgevlktx.html