Android Architecture Component之LiveData

前言

系列文章

一、liveData是什么?

1.介紹

  • LiveData是一個數據持有者類,他持有一個允許被觀察的值,不同于普通的被觀察者,liveData遵從應用程序的生命周期,被注冊的觀察者都要遵循其生命周期。
  • 如果觀察者的生命周期處于started或者resumed狀態的時候,liveData認為觀察者處于活動狀態。
    LiveData只通知處于活躍狀態的observer,不活躍的不通知其改變。
  1. 優點
  • 沒有內存泄漏的風險,當頁面銷毀的時候,他們會自動被移除,不會導致內存溢出
  • 不會因為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<T> extends LiveData<T> {
    @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),也有我們上篇講到到用注解自定義的觀察者(處理與生命周期有關事件的))。

  1. 什么時候通知觀察者?
  • 有人也可能會問為什么我們要把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。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,797評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,179評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 175,628評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,642評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,444評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,948評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,040評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,185評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,717評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,602評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,794評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,316評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,045評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,418評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,671評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,414評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,750評論 2 370