前言
系列文章
一、liveData是什么?
1.介紹
- LiveData是一個數據持有者類,他持有一個允許被觀察的值,不同于普通的被觀察者,liveData遵從應用程序的生命周期,被注冊的觀察者都要遵循其生命周期。
- 如果觀察者的生命周期處于started或者resumed狀態的時候,liveData認為觀察者處于活動狀態。
LiveData只通知處于活躍狀態的observer,不活躍的不通知其改變。
- 優點
- 沒有內存泄漏的風險,當頁面銷毀的時候,他們會自動被移除,不會導致內存溢出
- 不會因為activity的不可見導致crash。
- 當Activity不可見的時候,即使有數據發生改變,LiveData也不同通知觀察者,因為磁力的觀察者的生命周期處于Started或者Resumed狀態
- 配置的改變。
- 當前的Activity配置發生改變(如屏幕方向,導致生命周期重新走了一遍,但是觀察者們會恢復到變化前的數據。
- 資源共享
- 我們的LiveData,只要連接系統服務一次就能支持所有的觀察者。
- 不在手動處理生命周期
- 生命周期組件,只需要在觀察數據的時候觀察數據即可,不需要理會生命周期,這一切就交給類liveData.
- 總是能獲取最新的數據
- 當Activity從后臺進入前臺的時候總共能夠獲取最新的數據。
二、用法簡介
- 添加依賴
compile "android.arch.lifecycle:runtime:1.0.3" compile "android.arch.lifecycle:extensions:1.0.0-rc1" annotationProcessor "android.arch.lifecycle:compiler:1.0.0-rc1"
- 創建一個LiveData的實例來保存特定類型的數據。 這通常在ViewModel類中完成。
MutableLiveData類公開公開setValue(T)和postValue(T)方法,如果需要編輯存儲在LiveData對象中的值,則必須使用這些方法。 通常在ViewModel使用MutableLiveData ,然后ViewModel僅向觀察者公開不可變的LiveData對象。public class MainViewModel extends AndroidViewModel { private int i=10; private MutableLiveData<Student> studentMutableLiveData =new MutableLiveData<>(); public MainViewModel(Application application) { super(application); } public void changeName(Student student) { student.setAge(i++); studentMutableLiveData.setValue(student); } public MutableLiveData<Student> getStudentMutableLiveData() { return studentMutableLiveData; } }
- 在Activity中
每次點擊之后都會調用LiveData的setValue()方法,注冊的觀察者在onChanged()方法中就會收到更新后的我們觀察的數據student.ViewModelProvider of = ViewModelProviders.of(this); mainViewModel = of.get(MainViewModel.class); mainViewModel.getStudentMutableLiveData().observe(this, new Observer<Student>() { @Override public void onChanged(@Nullable Student student) { } }); findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mainViewModel.changeName(mstudent); } });
三、源碼分析
我們主要從三個方面講解:怎么添加觀察者?什么時候通知調用觀察者,怎么移除觀察者?怎么判定是liveData是活動活動狀態?
- 怎么添加觀察者?
- 上面的列子用到了MutableLiveData,看下他的源碼
很簡單,繼承了LiveData,提供了兩個修改我們要觀察數據的值。public class MutableLiveData<T> extends LiveData<T> { @Override public void postValue(T value) { super.postValue(value); } @Override public void setValue(T value) { super.setValue(value); } }
注意: 您必須調用setValue(T)方法來更新主線程中的LiveData對象。如果代碼在工作線程中執行,則可以使用postValue(T)方法更新LiveData對象。
- 從上面的列子中我們看到了MutableLiveData調用了observe()方法,這個方法是干什么用的呢?observer()繼承于LiveData,所以我們看下LiveData的Observer()
@MainThread public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) { if (owner.getLifecycle().getCurrentState() == DESTROYED) { // ignore return; } LifecycleBoundObserver wrapper = new LifecycleBoundObserver(owner, observer); LifecycleBoundObserver existing = mObservers.putIfAbsent(observer, wrapper); if (existing != null && existing.owner != wrapper.owner) { throw new IllegalArgumentException("Cannot add the same observer" + " with different lifecycles"); } if (existing != null) { return; } owner.getLifecycle().addObserver(wrapper); }
LifecycleOwner在上面的列子中我們傳入的是this,上篇我們說Lifecycle-Aware Components的時候我們知道activity實現了LifecycleOwner(繼承了class SupportActivity extends Activity implements LifecycleOwner )有個方法getLifecycle()返回了一個LifecycleRegistry的被觀察者,它的主要作用是組件生命周期通知注冊的觀察者,做出改變。
方法進來之后,首先獲取LifecycleRegistry中當前組件的狀態,如果處于destoryed,就什么也不做。
如果不是destroyed就把我們的觀察者封裝成了LifecycleBoundObserver(),
然后判斷我們LiveData中的觀察者mObservers集合中有咩有,沒有的話就放入,有的話就返回空。
如果沒有的話也放入我們Activity組件中LifecycleRegistry中觀察者集合中(這里面的觀察者很雜,有觀察數據的觀察者(屬于liveData),也有我們上篇講到到用注解自定義的觀察者(處理與生命周期有關事件的))。
- 什么時候通知觀察者?
-
有人也可能會問為什么我們要把liveData中的觀察者注冊到LifecycleRegistry的handleLifecycleEvent()中,它自己不是有自己的觀察者集合嗎?
這是因為在上篇我們將組件的生命周期事件分發的時候講到過通過LifecycleRegistry的handleLifecycleEvent()方法,而它被通過Activity中的ReportFragment的各個生命周期方法調用,所以我們要把我們的觀察者注冊到LifecycleRegistry,交由它去負責生命周期事件分發給我們的觀察者。
-
注冊到LifecycleRegistry中的時候會被封裝成ObserverWithState觀察者,有生命周期事件的時候會調用ObserverWithState的dispatchEvent()
ObserverWithState(LifecycleObserver observer, State initialState) { mLifecycleObserver = Lifecycling.getCallback(observer); mState = initialState; } void dispatchEvent(LifecycleOwner owner, Event event) { State newState = getStateAfter(event); mState = min(mState, newState); mLifecycleObserver.onStateChanged(owner, event); mState = newState; }
其中傳入的LifecycleObserver就是我們上面講的LifecycleBoundObserver,我們看到代碼中的dispatchEvent(),會調用onStateChanged,他怎么寫的呢?
@Override public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) { if (owner.getLifecycle().getCurrentState() == DESTROYED就一處,不是就通知改變) { removeObserver(observer); return; } // immediately set active state, so we'd never dispatch anything to inactive // owner activeStateChanged(isActiveState(owner.getLifecycle().getCurrentState())); }
若果當前的LifecycleRegistry的狀態是DESTROYED就移除我們的觀察者,這就知道什么時候移除,我們也就不需要擔心內存泄漏的風險。不是就通知改變,怎么通知觀察者的呢?我們看下activeStateChanged,傳入的參數我們后面會提到,先看下它的源碼。
if (active) { dispatchingValue(this); }
如果處于活動狀態就調用dispatchingValue()
for (Iterator<Map.Entry<Observer<T>, LifecycleBoundObserver>> iterator = mObservers.iteratorWithAdditions(); iterator.hasNext(); ) { for (Iterator<Map.Entry<Observer<T>, LifecycleBoundObserver>> iterator = mObservers.iteratorWithAdditions(); iterator.hasNext(); ) { considerNotify(iterator.next().getValue()); if (mDispatchInvalidated) { break; } }(iterator.next().getValue()); if (mDispatchInvalidated) { break; } }
在這里遍歷liveData存放觀察者的集合,然后considerNotify().看下源碼
observer.observer.onChanged()((T) mData);
onChanged()方法就是我們在用法中最終回調的方法。
我們總結一下過程:
- liveData的observer(),把我們的觀察者封裝成了LifecycleBoundObserver,這個是liveData專用的觀察者。
- 添加到LiveData的觀察者集合和Activity中的LifecycleRegistry的集合(負責生命周期分發,和獲取當前activit狀態)
- 當Activity狀態發生改變,就會通知我們的了LifecycleBoundObserver,同時調用它的onStateChanged()
- 然后在調用activeStateChanged(isActiveState(owner.getLifecycle().getCurrentState()));
- 然后遍歷liveData的觀察集合最后知道調用observer.observer.onChanged()((T) mData);
-
怎么判斷liveData是出于活躍狀態?
在文章的開頭部分我們提到:如果觀察者的生命周期處于started或者resumed狀態的時候,liveData認為觀察者處于活動狀態。
代碼中怎么體現出來的呢?
- 在上面的帶面中我們分析到了
其中有個關鍵方法就是activeStateChanged( isActiveState(owner.getLifecycle().getCurrentState()));
看下它的源碼isActiveState(owner.getLifecycle().getCurrentState())
關鍵點就在State,看下它的源碼static boolean isActiveState(State state) { return state.isAtLeast(STARTED); }
對于isAtLeast()方法調用了枚舉的compareTo,參數傳入進來的是STARTED,大于等于就剩STARTED,RESUMED,所以我們說LiveData活動狀態只有STARTED,RESUMED。public enum State { /** * Destroyed state for a LifecycleOwner. After this event, this Lifecycle will not dispatch * any more events. For instance, for an {@link android.app.Activity}, this state is reached * <b>right before</b> Activity's {@link android.app.Activity#onDestroy() onDestroy} call. */ DESTROYED, /** * Initialized state for a LifecycleOwner. For an {@link android.app.Activity}, this is * the state when it is constructed but has not received * {@link android.app.Activity#onCreate(android.os.Bundle) onCreate} yet. */ INITIALIZED, /** * Created state for a LifecycleOwner. For an {@link android.app.Activity}, this state * is reached in two cases: * <ul> * <li>after {@link android.app.Activity#onCreate(android.os.Bundle) onCreate} call; * <li><b>right before</b> {@link android.app.Activity#onStop() onStop} call. * </ul> */ CREATED, /** * Started state for a LifecycleOwner. For an {@link android.app.Activity}, this state * is reached in two cases: * <ul> * <li>after {@link android.app.Activity#onStart() onStart} call; * <li><b>right before</b> {@link android.app.Activity#onPause() onPause} call. * </ul> */ STARTED, /** * Resumed state for a LifecycleOwner. For an {@link android.app.Activity}, this state * is reached after {@link android.app.Activity#onResume() onResume} is called. */ RESUMED; /** * Compares if this State is greater or equal to the given {@code state}. * * @param state State to compare with * @return true if this State is greater or equal to the given {@code state} */ public boolean isAtLeast(@NonNull State state) { return compareTo(state) >= 0; } }