Jetpack 源码解析—LiveData的使用及工作原理

 
 

code小生,一个专注 Android 领域的技术平台

公众号回复 Android 加入我的安卓技术群

作者:Hankkinn
链接:https://juejin.im/post/5d247b036fb9a07eee5ef3df
声明:本文已获
Hankkinn授权发表,转发等请联系原作者授权

1. 背景

上一篇我们分析了Lifecycles组件的源码,本篇我们将继续分析LiveData组件

相关系列文章:

  1. Jetpack源码解析---看完你就知道Navigation是什么了?
    https://juejin.im/post/5d1202fc6fb9a07ef90ca7a1

  2. Jetpack源码解析---Navigation为什么切换Fragment会重绘?
    https://juejin.im/post/5d12cfea6fb9a07ed4411040

  3. Jetpack源码解析---用Lifecycles管理生命周期https://juejin.im/post/5d15bbb86fb9a07f03574e56

2. 基础

2.1 简介

LiveData是一个可观察的数据持有者类,与常规observable不同,LiveData是生命周期感知的,这意味着它尊重其他应用程序组件的生命周期,例如ActivityFragmentService。此感知确保LiveData仅更新处于活动生命周期状态的应用程序组件观察者。

2.2  优点

1. 确保UI符合数据状态
LiveData遵循观察者模式。当生命周期状态改变时,LiveData会向Observer发出通知。您可以把更新UI的代码合并在这些Observer对象中。不必去考虑导致数据变化的各个时机,每次数据有变化,Observer都会去更新UI。

2. 没有内存泄漏
Observer会绑定具有生命周期的对象,并在这个绑定的对象被销毁后自行清理。

3. 不会因停止Activity而发生崩溃
如果Observer的生命周期处于非活跃状态,例如在后退堆栈中的Activity,就不会收到任何LiveData事件的通知。

4.不需要手动处理生命周期
UI组件只需要去观察相关数据,不需要手动去停止或恢复观察。LiveData会进行自动管理这些事情,因为在观察时,它会感知到相应组件的生命周期变化。

5. 始终保持最新的数据
如果一个对象的生命周期变到非活跃状态,它将在再次变为活跃状态时接收最新的数据。例如,后台Activity在返回到前台后立即收到最新数据。

6. 正确应对配置更改
如果一个Activity或Fragment由于配置更改(如设备旋转)而重新创建,它会立即收到最新的可用数据。

7.共享资源
您可以使用单例模式扩展LiveData对象并包装成系统服务,以便在应用程序中进行共享。LiveData对象一旦连接到系统服务,任何需要该资源的Observer都只需观察这个LiveData对象。

2.3 基本使用

在我们的Jetpack_Note中有使用demo,具体可查看LiveDataFragment。
Demo中通过对一个LiveData对象进行生命周期的监听,实现将值打印在控制台中。首先声明一个LiveData对象:

private lateinit var liveData: MutableLiveData<String>lateinit var liveData: MutableLiveData<String>

点击开始观察数据按钮,弹出控制台,我们可以看到控制台输出了onStart()日志,因为我们将liveData的值和Fragment的生命周期进行了绑定,当返回桌面或者销毁Fragment的时候,LiveData的值会变成相应的生命周期函数,并打印在控制台中:

class LiveDataFragment : Fragment() {    private lateinit var liveData: MutableLiveData<String>    override fun onCreateView(        inflater: LayoutInflater, container: ViewGroup?,        savedInstanceState: Bundle?    ): View? {        return inflater.inflate(R.layout.fragment_live_data, container, false)    }    override fun onActivityCreated(savedInstanceState: Bundle?) {        super.onActivityCreated(savedInstanceState)        liveData = MutableLiveData()        btn_observer_data.setOnClickListener {            if (FloatWindow.get() == null) {                FloatWindowUtils.init(activity?.application!!)            }            FloatWindowUtils.show()            //创建一个观察者去更新UI            val statusObserver = Observer<String> { lifeStatus ->                FloatWindowUtils.addViewContent("LiveData-onChanged: $lifeStatus")            }            liveData.observeForever(statusObserver)        }    }    override fun onStart() {        super.onStart()        liveData.value = "onStart()"    }    override fun onPause() {        super.onPause()        liveData.value = "onPause()"    }    override fun onStop() {        super.onStop()        liveData.value = "onStop()"    }    override fun onDestroy() {        super.onDestroy()        liveData.value = "onDestroy()"    }}

    private lateinit var liveData: MutableLiveData<String>

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    )
: View? {
        return inflater.inflate(R.layout.fragment_live_data, container, false)
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        liveData = MutableLiveData()
        btn_observer_data.setOnClickListener {
            if (FloatWindow.get() == null) {
                FloatWindowUtils.init(activity?.application!!)
            }
            FloatWindowUtils.show()

            //创建一个观察者去更新UI
            val statusObserver = Observer<String> { lifeStatus ->
                FloatWindowUtils.addViewContent("LiveData-onChanged: $lifeStatus")
            }
            liveData.observeForever(statusObserver)
        }
    }


    override fun onStart() {
        super.onStart()
        liveData.value = "onStart()"
    }


    override fun onPause() {
        super.onPause()
        liveData.value = "onPause()"
    }

    override fun onStop() {
        super.onStop()
        liveData.value = "onStop()"
    }

    override fun onDestroy() {
        super.onDestroy()
        liveData.value = "onDestroy()"
    }
}

注意:这里我使用了observeForever监听了所有生命周期方法,所以你会看到onDestroy()等生命周期函数的打印。

好了,Demo很简单,接下来我们来看一下源码,进行分析:

3. 源码分析:

3.1 observer()

我们声明了一个LiveData对象,并通过监听Fragment的生命周期来改变LiveData中的value值,LiveData实际上就像一个容器,Demo中存储了一个String类型的值,当这个值发生改变的时候,可以在回调中监听到他的改变。接下来我们就先从addObserver入手:

@MainThread    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);        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;        }        owner.getLifecycle().addObserver(wrapper);    }
    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);
        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;
        }
        owner.getLifecycle().addObserver(wrapper);
    }

查看源码,我们调用observer()时,传递了两个参数,第一个是LifecycleOwner接口实例,而我们继承的Fragment本身就已经实现了这个接口,所以我们传this即可;第二个参数Observer就是我们观察的回调。接下来将这两个参数传递new出了一个新的对象:LifecycleBoundObserver,最后将LifecycleBoundObserverLifecycleOwner进行了绑定,其实这里面我们可以将LifecycleOwner就理解成我们的Fragment或者Activity的实例,因为它们都实现了LifecycleOwner

3.2 LifecycleBoundObserver

class LifecycleBoundObserver extends ObserverWrapper implements GenericLifecycleObserver {        .....        @Override        boolean shouldBeActive() {            return mOwner.getLifecycle().getCurrentState().isAtLeast(STARTED);        }        //Activity生命周期变化时,回调方法        @Override        public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {            if (mOwner.getLifecycle().getCurrentState() == DESTROYED) {                removeObserver(mObserver);                return;            }            //更新livedata活跃状态            activeStateChanged(shouldBeActive());        }        @Override        boolean isAttachedTo(LifecycleOwner owner) {            return mOwner == owner;        }        //解除监听        @Override        void detachObserver() {            mOwner.getLifecycle().removeObserver(this);        }    }
        .....
        @Override
        boolean shouldBeActive() {
            return mOwner.getLifecycle().getCurrentState().isAtLeast(STARTED);
        }
        //Activity生命周期变化时,回调方法
        @Override
        public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
            if (mOwner.getLifecycle().getCurrentState() == DESTROYED) {
                removeObserver(mObserver);
                return;
            }
            //更新livedata活跃状态
            activeStateChanged(shouldBeActive());
        }

        @Override
        boolean isAttachedTo(LifecycleOwner owner) {
            return mOwner == owner;
        }
        //解除监听
        @Override
        void detachObserver() {
            mOwner.getLifecycle().removeObserver(this);
        }
    }

我们可以看到这里面与LifecycleOwner进行了绑定,并且实现了onStateChanged方法,当生命周期发生变化时执行activeStateChanged(shouldBeActive());方法;shouldBeActive() 返回了 要求生命周期至少是STARTED状态才被认为是activie状态;如果state是DESTROYED状态时,解绑LifecycleOwnerLiveData。接下来我们看下怎样更新livedata中数据值:

3.3 dispatchingValue()

我们追踪activeStateChanged方法发现,在里面做了一些活跃状态值的操作,并且当状态活跃时,更新数据值:

void activeStateChanged(boolean newActive) {            if (newActive == mActive) {                return;            }            // immediately set active state, so we'd never dispatch anything to inactive            // owner            mActive = newActive;            boolean wasInactive = LiveData.this.mActiveCount == 0;            LiveData.this.mActiveCount += mActive ? 1 : -1;            if (wasInactive && mActive) {                onActive();            }            if (LiveData.this.mActiveCount == 0 && !mActive) {                onInactive();            }            if (mActive) {                dispatchingValue(this);            }        }
            if (newActive == mActive) {
                return;
            }
            // immediately set active state, so we'd never dispatch anything to inactive
            // owner
            mActive = newActive;
            boolean wasInactive = LiveData.this.mActiveCount == 0;
            LiveData.this.mActiveCount += mActive ? 1 : -1;
            if (wasInactive && mActive) {
                onActive();
            }
            if (LiveData.this.mActiveCount == 0 && !mActive) {
                onInactive();
            }
            if (mActive) {
                dispatchingValue(this);
            }
        }
void dispatchingValue(@Nullable ObserverWrapper initiator) {    ...    //遍历LiveData的所有观察者执行下面代码    for (Iterator<Map.Entry<Observer<? super T>, ObserverWrapper>> iterator =                        mObservers.iteratorWithAdditions(); iterator.hasNext(); ) {                    considerNotify(iterator.next().getValue());                    if (mDispatchInvalidated) {                        break;                    }                }    ...}//将数据值回调到livedata.observer()回去private void considerNotify(ObserverWrapper observer) {        if (!observer.mActive) {            return;        }        if (!observer.shouldBeActive()) {            observer.activeStateChanged(false);            return;        }        if (observer.mLastVersion >= mVersion) {            return;        }        observer.mLastVersion = mVersion;        observer.mObserver.onChanged((T) mData);    }
    ...
    //遍历LiveData的所有观察者执行下面代码
    for (Iterator<Map.Entry<Observer<? super T>, ObserverWrapper>> iterator =
                        mObservers.iteratorWithAdditions(); iterator.hasNext(); ) {
                    considerNotify(iterator.next().getValue());
                    if (mDispatchInvalidated) {
                        break;
                    }
                }
    ...
}
//将数据值回调到livedata.observer()回去
private void considerNotify(ObserverWrapper observer) {
        if (!observer.mActive) {
            return;
        }
        if (!observer.shouldBeActive()) {
            observer.activeStateChanged(false);
            return;
        }
        if (observer.mLastVersion >= mVersion) {
            return;
        }
        observer.mLastVersion = mVersion;
        observer.mObserver.onChanged((T) mData);
    }

从上面我们可以看到LiveData的数据更新以及数据回调的整个过程,但是当我们手动setValue()的时候过程是怎样的呢?你会发现,setValue()其实最后就是通过调用了dispatchingValue()方法。而关于postValue()在子线程更新数据的相关代码,这里就不做介绍了,其实你大可以想出来,就是使用的Handler

LiveData中的代码很简洁,400多行的代码,看起来也并不费劲,下面我们来分析下整个流程:

4. 扩展

4.1 Map转换

我们在开发中经常会遇到这种场景,有时我们需要根据另外一个LiveData实例返不同的LiveData实例,然后在分发给Observer,Lifecycle包提供了Transformations类,可以帮助我们实现这样的场景:

通过Transformations.map()使用一个函数来转换存储在 LiveData对象中的值,并向下传递转换后的值:

LiveDataViewModel

class LiveDataViewModel : ViewModel() {    val data = listOf(User(0,"Hankkin"), User(1,"Tony"),User(2,"Bob"),User(3,"Lucy"))    val id = MutableLiveData<Int>()    //map转换返回User实体    val bean: LiveData<User> = Transformations.map(id, Function {        return@Function findUserById(id.value!!)    })    //根据id查找User    private fun findUserById(id: Int): User? {        return data.find { it.id == id }    }}
    val data = listOf(User(0,"Hankkin"), User(1,"Tony"),User(2,"Bob"),User(3,"Lucy"))

    val id = MutableLiveData<Int>()
    //map转换返回User实体
    val bean: LiveData<User> = Transformations.map(id, Function {
        return@Function findUserById(id.value!!)
    })
    //根据id查找User
    private fun findUserById(id: Int): User? {
        return data.find { it.id == id }
    }
}

LiveDataFragment

//改变ViewModel中idLiveData中的值        btn_observer_map.setOnClickListener {            mId++            viewModel.id.postValue(mId)        }        //当idLiveData变化后,UserBean也会改变且更新Textview的文本        viewModel.bean.observe(            this,            Observer { tv_livedata_map.text = if (it == null) "未查找到User" else "为你查找到的User为:${it.name}" })
        btn_observer_map.setOnClickListener {
            mId++
            viewModel.id.postValue(mId)
        }
        //当idLiveData变化后,UserBean也会改变且更新Textview的文本
        viewModel.bean.observe(
            this,
            Observer { tv_livedata_map.text = if (it == null"未查找到User" else "为你查找到的User为:${it.name}" })

4.2 Map源码

@MainThread    public static <X, Y> LiveData<Y> map(            @NonNull LiveData<X> source,            @NonNull final Function<X, Y> mapFunction) {        final MediatorLiveData<Y> result = new MediatorLiveData<>();        result.addSource(source, new Observer<X>() {            @Override            public void onChanged(@Nullable X x) {                result.setValue(mapFunction.apply(x));            }        });        return result;    }
    public static <X, Y> LiveData<Y> map(
            @NonNull LiveData<X> source,
            @NonNull final Function<X, Y> mapFunction)
 
{
        final MediatorLiveData<Y> result = new MediatorLiveData<>();
        result.addSource(source, new Observer<X>() {
            @Override
            public void onChanged(@Nullable X x) {
                result.setValue(mapFunction.apply(x));
            }
        });
        return result;
    }

我们可以看到map的源码是通过MediatorLiveData中的addSource()方法来实现的,第一个参数为我们需要改变的LiveData值,也就是我们上面例子中的userid,第二个参数则是我们传过来的Fuction通过高阶函数,将值set到LiveData上。

下面我们看下addSource()方法:

@MainThread    public <S> void addSource(@NonNull LiveData<S> source, @NonNull Observer<? super S> onChanged) {        Source<S> e = new Source<>(source, onChanged);        Source<?> existing = mSources.putIfAbsent(source, e);        if (existing != null && existing.mObserver != onChanged) {            throw new IllegalArgumentException(                    "This source was already added with the different observer");        }        if (existing != null) {            return;        }        if (hasActiveObservers()) {            e.plug();        }    }
    public <S> void addSource(@NonNull LiveData<S> source, @NonNull Observer<? super S> onChanged) {
        Source<S> e = new Source<>(source, onChanged);
        Source<?> existing = mSources.putIfAbsent(source, e);
        if (existing != null && existing.mObserver != onChanged) {
            throw new IllegalArgumentException(
                    "This source was already added with the different observer");
        }
        if (existing != null) {
            return;
        }
        if (hasActiveObservers()) {
            e.plug();
        }
    }

这里把我们的LiveData和Observer封装成了Source对象,并且这个对象,不能重复添加,具体代码可查看Source:

private static class Source<V> implements Observer<V> {        final LiveData<V> mLiveData;        final Observer<? super V> mObserver;        int mVersion = START_VERSION;        Source(LiveData<V> liveData, final Observer<? super V> observer) {            mLiveData = liveData;            mObserver = observer;        }        void plug() {            mLiveData.observeForever(this);        }        void unplug() {            mLiveData.removeObserver(this);        }        @Override        public void onChanged(@Nullable V v) {            if (mVersion != mLiveData.getVersion()) {                mVersion = mLiveData.getVersion();                mObserver.onChanged(v);            }        }    }static class Source<Vimplements Observer<V{
        final LiveData<V> mLiveData;
        final Observer<? super V> mObserver;
        int mVersion = START_VERSION;

        Source(LiveData<V> liveData, final Observer<? super V> observer) {
            mLiveData = liveData;
            mObserver = observer;
        }

        void plug() {
            mLiveData.observeForever(this);
        }

        void unplug() {
            mLiveData.removeObserver(this);
        }

        @Override
        public void onChanged(@Nullable V v) {
            if (mVersion != mLiveData.getVersion()) {
                mVersion = mLiveData.getVersion();
                mObserver.onChanged(v);
            }
        }
    }

首先Source是一个观察者,可以看到,我们外部使用的Observer会以Source的成员变量的形式,添加到传入的LiveData中。值得注意的是,这里使用了mLiveData.observeForever(this);

observeForever()用法可以看到,我们并没有传递LifecycleOwner,因此它并不具备生命感知能力。从注释中也可见一斑:This means that the given observer will receive all events and will never be automatically removed.

map()的原理就是基于MediatorLiveData,MediatorLiveData内部会将传递进来的LiveData和Observer封装成内部类,然后放在内部维护的一个Map中。并且自动帮我们完成observeForever()和removeObserver()

5.总结

当然官方推荐我们LiveData配合ViewModel一起使用,因为LiveData一般都出现在ViewModel中,所以我们下篇文章会继续分析ViewModel.

推荐阅读
Android Jetpack - 使用 Navigation 管理页面跳转
Android Jetpack - 使用 WorkManager 管理后台任务

Android 官方架构组件(二)——LiveData

LiveData 源码分析之事件总线 LiveBus 实现


640?wx_fmt=jpeg

扫一扫 关注我的公众号

如果你想要跟大家分享你的文章,欢迎投稿~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值