1. 程式人生 > >Android Architecture Component之LiveData

Android Architecture Component之LiveData

前言
一、liveData是什麼?

1.介紹
* LiveData是一個數據持有者類,他持有一個允許被觀察的值,不同於普通的被觀察者,liveData遵從應用程式的生命週期,被註冊的觀察者都要遵循其生命週期。
* 如果觀察者的生命週期處於started或者resumed狀態的時候,liveData認為觀察者處於活動狀態。
LiveData只通知處於活躍狀態的observer,不活躍的不通知其改變。
2. 優點
* 沒有記憶體洩漏的風險,當頁面銷燬的時候,他們會自動被移除,不會導致記憶體溢位
* 不會因為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類中完成。

    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; } }

    MutableLiveData類公開公開setValue(T)和postValue(T)方法,如果需要編輯儲存在LiveData物件中的值,則必須使用這些方法。 通常在ViewModel使用MutableLiveData ,然後ViewModel僅向觀察者公開不可變的LiveData物件。

    • 在Activity中

      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的setValue()方法,註冊的觀察者在onChanged()方法中就會收到更新後的我們觀察的資料student.

三、原始碼分析

我們主要從三個方面講解:怎麼新增觀察者?什麼時候通知呼叫觀察者,怎麼移除觀察者?怎麼判定是liveData是活動活動狀態?
1. 怎麼新增觀察者?
* 上面的列子用到了MutableLiveData,看下他的原始碼
“`
public class MutableLiveData extends LiveData {
@Override
public void postValue(T value) {
super.postValue(value);
}

@Override
public void setValue(T value) {
    super.setValue(value);
}
}
```
很簡單,繼承了LiveData,提供了兩個修改我們要觀察資料的值。
>  注意: 您必須呼叫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),也有我們上篇講到到用註解自定義的觀察者(處理與生命週期有關事件的))。

2. 什麼時候通知觀察者?
* 有人也可能會問為什麼我們要把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);
  1. 怎麼判斷liveData是出於活躍狀態?
    在文章的開頭部分我們提到:

    如果觀察者的生命週期處於started或者resumed狀態的時候,liveData認為觀察者處於活動狀態。

    程式碼中怎麼體現出來的呢?

    • 在上面的帶面中我們分析到了
      activeStateChanged(
      isActiveState(owner.getLifecycle().getCurrentState()));

    其中有個關鍵方法就是

    isActiveState(owner.getLifecycle().getCurrentState())

    看下它的原始碼

    static boolean isActiveState(State state) {
        return state.isAtLeast(STARTED);
    }

    關鍵點就在State,看下它的原始碼

    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;
        }
    }

    對於isAtLeast()方法呼叫了列舉的compareTo,引數傳入進來的是STARTED,大於等於就剩STARTED,RESUMED,所以我們說LiveData活動狀態只有STARTED,RESUMED。