Android Jetpack之LiveData概述

Android Jetpack之LiveData概述

概述

LiveData是一個可被觀察的數據持有者類。與常規的Observable不同,LiveData能意識到應用程序組件的生命週期變化,這意味著它能遵守Activity、Fragment、Service等組件的生命週期。這種意識確保LiveData只更新處於活躍狀態的應用程序組件Observer。

Note: 如何在Android項目中引入LiveData組件, 請參閱 添加項目組件。

如果一個Observer的生命週期處於STARTED或RESUMED狀態,那麼LiveData將認為這個Observer處於活躍狀態。 LiveData僅通知活躍的Observer去更新UI。 非活躍狀態的Observer,即使訂閱了LiveData,也不會收到更新的通知。

結合一個實現了LifecycleOwner接口的對象,你能註冊一個Observer。這種結合關係使得當具有生命週期的對象的狀態變為DESTROYED時,Observer將被取消訂閱。 這對於Activity和Fragment尤其有用,因為它們可以安全地訂閱LiveData對象,而不必擔心內存洩漏 - 當Activity和Fragment生命週期為DESTROYED時,它們立即會被取消訂閱。

有關如何使用LiveData的更多信息,請參閱 使用LiveData對象。

LiveData的優點

在項目中使用LiveData,會有以下優點:

  • 確保UI符合數據狀態
  • LiveData遵循觀察者模式。 當生命週期狀態改變時,LiveData會向Observer發出通知。 您可以把更新UI的代碼合併在這些Observer對象中。不必去考慮導致數據變化的各個時機,每次數據有變化,Observer都會去更新UI。
  • 沒有內存洩漏
  • Observer會綁定具有生命週期的對象,並在這個綁定的對象被銷燬後自行清理。
  • 不會因停止Activity而發生崩潰
  • 如果Observer的生命週期處於非活躍狀態,例如在後退堆棧中的Activity,就不會收到任何LiveData事件的通知。
  • 不需要手動處理生命週期
  • UI組件只需要去觀察相關數據,不需要手動去停止或恢復觀察。LiveData會進行自動管理這些事情,因為在觀察時,它會感知到相應組件的生命週期變化。
  • 始終保持最新的數據
  • 如果一個對象的生命週期變到非活躍狀態,它將在再次變為活躍狀態時接收最新的數據。 例如,後臺Activity在返回到前臺後立即收到最新數據。
  • 正確應對配置更改
  • 如果一個Activity或Fragment由於配置更改(如設備旋轉)而重新創建,它會立即收到最新的可用數據。
  • 共享資源
  • 您可以使用單例模式擴展LiveData對象幷包裝成系統服務,以便在應用程序中進行共享。LiveData對象一旦連接到系統服務,任何需要該資源的Observer都只需觀察這個LiveData對象。 有關更多信息,請參閱擴展LiveData。

使用LiveData對象

按照以下步驟使用LiveData對象:

  1. 創建一個LiveData的實例來保存特定類型的數據。 這通常在ViewModel類中完成。
  2. 創建一個定義了onChanged()方法的Observer對象,當LiveData對象保存的數據發生變化時,onChanged()方法可以進行相應的處理。 您通常在UI控制器(如Activity或Fragment)中創建Observer對象。
  3. 使用observe()方法將Observer對象註冊到LiveData對象。 observe()方法還需要一個LifecycleOwner對象作為參數。 Observer對象訂閱了LiveData對象,便會在數據發生變化時發出通知。 您通常需要UI控制器(如Activity或Fragment)中註冊Observer對象。

Note:您可以使用observeForever(Observer)方法註冊一個沒有關聯LifecycleOwner對象的Observer。 在這種情況下,Observer被認為始終處於活動狀態,因此當有數據變化時總是會被通知。 您可以調用removeObserver(Observer)方法移除這些Observer。

當你更新LiveData對象中存儲的數據時,所有註冊了的Observer,只要所綁定的LifecycleOwner處於活動狀態,就會被觸發通知。

LiveData允許UI控制器Observer訂閱更新。 當LiveData對象所保存的數據發生變化時,UI會在響應中自動更新。

創建LiveData對象

LiveData是一個包裝器,可用於任何數據,包括實現Collections的對象,如List。一個 LiveData對象通常存儲在ViewModel對象中,並通過getter方法訪問,如以下示例所示:

public class NameViewModel extends ViewModel {
// Create a LiveData with a String
private MutableLiveData<string> mCurrentName;
public MutableLiveData<string> getCurrentName() {
if (mCurrentName == null) {
mCurrentName = new MutableLiveData<string>();
}
return mCurrentName;
}
// Rest of the ViewModel...
}
/<string>/<string>/<string>

起初,LiveData對象中的數據未設置。

Note:確保在ViewModel而不是Activity或Fragment中保存用來更新UI的LiveData對象,原因如下:

  • 避免臃腫的Activity和Fragment。 這些UI控制器負責顯示數據而不是保存數據狀態。
  • 將LiveData實例與特定Activity或Fragment實例分離,這將使得LiveData對象在配置更改後仍然存活。

有關ViewModel更多的用處和益處,請參閱Android應用結構之ViewModel。

觀察LiveData對象

在大多數情況下,出於以下原因,應用程序組件的onCreate()方法是開始觀察LiveData對象的最佳位置:

  1. 確保系統不會從Activity或Fragment的onResume()方法中進行多餘的調用。
  2. 確保Activity或Fragment一旦變為活動狀態時,就有可展示的數據。 當應用程序組件處於STARTED狀態,它就需從它所觀察的LiveData對象中接收到最新的值。 所以我們需要在一開始就設置好觀察。

通常情況下,LiveData只在數據有變化時,給活躍的Observer進行通知。 此行為的一個例外是,Observer在從非活躍狀態變為活躍狀態時也會收到通知。 並且,如果Observer第二次從非活躍狀態變為活躍狀態,則只有在自上一次變為活躍狀態以來該數據發生變化時才會接收到更新。

以下示例代碼演示瞭如何開始觀察LiveData對象:

public class NameActivity extends AppCompatActivity {
private NameViewModel mModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Other code to setup the activity...
// Get the ViewModel.
mModel = ViewModelProviders.of(this).get(NameViewModel.class);
// Create the observer which updates the UI.
final Observer<string> nameObserver = new Observer<string>() {
@Override
public void onChanged(@Nullable final String newName) {
// Update the UI, in this case, a TextView.
mNameTextView.setText(newName);
}
};
// Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
mModel.getCurrentName().observe(this, nameObserver);
}
}

/<string>/<string>

傳遞nameObserver作為參數,調用observe()之後,將立即回調onChanged(),提供存儲在mCurrentName中的最新值。 如果LiveData對象mCurrentName的值並未設置,則不調用onChanged()。

更新LiveData對象

LiveData沒有公用的方法來更新存儲的數據。 MutableLiveData類暴露公用的setValue(T)和postValue(T)方法,如果需要編輯存儲在LiveData對象中的值,必須使用這兩個方法。 通常在ViewModel中使用MutableLiveData,然後ViewModel僅向Observer公開不可變的LiveData對象。

在建立觀察者關係之後,可以更新LiveData對象的值,如以下示例所示,當用戶點擊按鈕時向所有觀察者發出通知:

mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String anotherName = "John Doe";
mModel.getCurrentName().setValue(anotherName);
}
});

在示例中調用setValue(T)會導致Observer使用值"John Doe"調用onChanged()方法。 這個例子展示了點擊按鈕,setValue()或者postValue()被調用來更新mName,原因有多種,包括響應網絡請求或數據庫加載完成; 在所有情況下,調用setValue()或postValue()都會觸發觀察者並更新UI。

Note: 必須要從主線程調用setValue(T) 方法來更新LiveData 對象. 如果代碼在工作線程中執行, 你可以使用postValue(T) 方法來更新LiveData對象.

LiveData配合Room使用

Room持久性庫支持Observable查詢返回LiveData對象。 Observable查詢成為數據庫訪問對象(DAO)的一項功能。

當更新數據庫時,會生成所有必要的代碼來更新LiveData對象。 生成的代碼在需要時在後臺線程上異步運行查詢。 這種模式對於保持用戶界面中顯示的數據與存儲在數據庫中的數據同步很有用。 您可以在Room持久性庫指南中閱讀關於Room和DAO的更多信息。

擴展LiveData

如果Observer的生命週期處於STARTED或RESUMED狀態,則LiveData將認為Observer處於活動狀態。以下示例代碼說明了如何擴展LiveData類:

public class StockLiveData extends LiveData<bigdecimal> {
private StockManager mStockManager;
private SimplePriceListener mListener = new SimplePriceListener() {
@Override
public void onPriceChanged(BigDecimal price) {
setValue(price);
}
};
public StockLiveData(String symbol) {
mStockManager = new StockManager(symbol);
}
@Override
protected void onActive() {
mStockManager.requestPriceUpdates(mListener);
}
@Override
protected void onInactive() {

mStockManager.removeUpdates(mListener);
}
}
/<bigdecimal>

本例中StockLiveData的實現包括以下重要的方法:

  • 當LiveData對象有一個活躍的Observer時,onActive()方法被調用。 這意味著你需要從這個方法開始觀察股票價格的更新。
  • 當LiveData對象沒有任何活躍的Observer時,onInactive()方法被調用。 由於沒有Observer在監聽,所以沒有理由繼續保持與StockManager服務的連接。
  • setValue(T)方法更新LiveData實例的值,並通知活動觀察者有關更改。
  • 您可以這樣使用StockLiveData類,如下所示:
public class MyFragment extends Fragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LiveData<bigdecimal> myPriceListener = ...;
myPriceListener.observe(this, price -> {
// Update the UI.
});
}
}
/<bigdecimal>

observe()方法第一個參數是作為LifecycleOwner實例的Fragment。 這樣做表示此Observer綁定了Lifecycle對象的生命週期,即:

  • 如果Lifecycle對象不處於活動狀態,則即使值發生更改,也不會調用Observer。
  • Lifecycle對象被銷燬後,Observer被自動刪除。
  • LiveData對象具有感知生命週期的能力意味著您可以在多個Activity,Fragment和service之間共享它們。 為了保持簡潔,你可以使用單例模式實現LiveData,如下所示:
public class StockLiveData extends LiveData<bigdecimal> {
private static StockLiveData sInstance;
private StockManager mStockManager;
private SimplePriceListener mListener = new SimplePriceListener() {
@Override
public void onPriceChanged(BigDecimal price) {
setValue(price);
}
};
@MainThread
public static StockLiveData get(String symbol) {
if (sInstance == null) {
sInstance = new StockLiveData(symbol);
}
return sInstance;
}
private StockLiveData(String symbol) {
mStockManager = new StockManager(symbol);
}
@Override
protected void onActive() {
mStockManager.requestPriceUpdates(mListener);
}
@Override
protected void onInactive() {
mStockManager.removeUpdates(mListener);
}
}
/<bigdecimal>

你可以在Fragment中使用它,如下所示:

public class MyFragment extends Fragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
StockLiveData.get(getActivity()).observe(this, price -> {

// Update the UI.
});
}
}

多個Activity和Fragment可以觀察到MyPriceListener實例。 LiveData只在他們至少一個處於可見和活躍狀態時才連接到系統服務。

轉換LiveData

您可能希望先轉換存儲在LiveData對象中的值,然後再將其分派給Observer,或者您可能需要根據一個LiveData實例的值返回不同的LiveData實例。Lifecycle包提供了 Transformations類,提供了支持這些使用場景的方法。

Transformations.map()

使用一個函數來轉換存儲在LiveData對象中的值,並向下傳遞轉換後的值。

LiveData<user> userLiveData = ...;
LiveData<string> userName = Transformations.map(userLiveData, user -> {
user.name + " " + user.lastName
});
/<string>/<user>

Transformations.switchMap()

與map()類似,使用一個函數來轉換存儲在LiveData對象中的值,並向下傳遞結果。 傳遞給switchMap()的函數必須返回一個LiveData對象,如下例所示:

private LiveData<user> getUser(String id) {
...;
}

LiveData<string> userId = ...;
LiveData<user> user = Transformations.switchMap(userId, id -> getUser(id) );
/<user>/<string>/<user>

您可以使用轉換方法在Observer的生命週期中傳遞信息。 除非Observer正在觀看返回的LiveData對象,否則不會計算轉換。 由於轉換是延遲計算的,所以與生命週期相關的行為隱式傳遞,而不需要額外的顯式調用或依賴關係。

如果您認為在ViewModel對象中需要Lifecycle對象,則轉換可能是更好的解決方案。 例如,假設您有一個接受地址並返回該地址的郵政編碼的UI組件。 您可以為此組件實現樸素的ViewModel,如以下示例代碼所示:

class MyViewModel extends ViewModel {
private final PostalCodeRepository repository;
public MyViewModel(PostalCodeRepository repository) {
this.repository = repository;
}
private LiveData<string> getPostalCode(String address) {
// DON'T DO THIS
return repository.getPostCode(address);
}
}
/<string>

UI組件隨後需要從以前的LiveData對象註銷,並在每次調用getPostalCode()時註冊到新實例。 另外,如果UI組件被重新創建,它會觸發對repository.getPostCode()方法的另一個調用,而不是使用前一個調用的結果。

相反,您可以實現郵政編碼查找作為地址輸入的轉換,如以下示例所示:

class MyViewModel extends ViewModel {
private final PostalCodeRepository repository;
private final MutableLiveData<string> addressInput = new MutableLiveData();
public final LiveData<string> postalCode =
Transformations.switchMap(addressInput, (address) -> {
return repository.getPostCode(address);
});
public MyViewModel(PostalCodeRepository repository) {
this.repository = repository
}
private void setInput(String address) {
addressInput.setValue(address);
}
}
/<string>/<string>

在這種情況下,postalCode字段是公開的和最終的,因為該字段永遠不會改變。 postalCode字段定義為addressInput的轉換,這意味著addressInput發生更改時,如果有一個活躍的Observer,將調用repository.getPostCode()方法,如果在repository.getPostCode()被調用時沒有活躍的Observer,直到添加一個觀察者才會進行計算。

此機制允許較低級別的應用程序創建按需延遲計算的LiveData對象。 ViewModel對象可以很容易地獲得對LiveData對象的引用,然後在其上定義轉換規則。

創建新的Transformations

我們有十幾個不同的具體Transformations,它們可能在你的應用程序中很有用,但是它們並不是默認提供的。 要實現自己的轉換,您可以使用MediatorLiveData類,該類監聽其他LiveData對象並處理它們發出的事件。 MediatorLiveData將其狀態正確地傳播到源LiveData對象。 要了解有關此模式的更多信息,請參閱Transformations的參考文檔。

合併多個LiveData源

MediatorLiveData是LiveData的一個子類,幫助您合併多個LiveData源。 在任何原始LiveData源對象改變後,MediatorLiveData對象的Observer會被觸發。

例如,如果在UI中有一個從本地數據庫或網絡獲取更新的LiveData對象,則可以將以下數據源添加到MediatorLiveData對象:

  • 與存儲在數據庫中的數據關聯的LiveData對象。
  • 與從網絡訪問的數據關聯的LiveData對象。
  • 您的Activity只需觀察MediatorLiveData對象即可接收來自兩個數據源的更新。 有關詳細示例,請參閱應用程序體系結構指南的附錄:Guide to App Architecture。

【附錄】

Android Jetpack之LiveData概述

資料圖

關注我私信回覆【學習】獲取免費學習視頻,學習大綱另外還有像高級UI、性能優化、架構師課程、NDK、混合式開發(ReactNative+Weex)等Android高階開發資料免費分享!


分享到:


相關文章: