PagingLiveData

class PagingLiveData


Summary

Public functions

LiveData<PagingData<T>>
<T : Any> LiveData<PagingData<T>>.cachedIn(lifecycle: Lifecycle)

Operator which caches a LiveData of PagingData within the scope of a Lifecycle.

LiveData<PagingData<T>>
<T : Any> LiveData<PagingData<T>>.cachedIn(viewModel: ViewModel)

Operator which caches a LiveData of PagingData within the scope of a ViewModel.

LiveData<PagingData<T>>

Operator which caches a LiveData of PagingData within a CoroutineScope.

Public properties

LiveData<PagingData<Value>>
Pager<Key, Value>.liveData

A LiveData of PagingData, which mirrors the stream provided by Pager.flow, but exposes it as a LiveData.

Public functions

cachedIn

fun <T : Any> LiveData<PagingData<T>>.cachedIn(lifecycle: Lifecycle): LiveData<PagingData<T>>

Operator which caches a LiveData of PagingData within the scope of a Lifecycle.

cachedIn multicasts pages loaded and transformed by a PagingData, allowing multiple observers on the same instance of PagingData to receive the same events, avoiding redundant work, but comes at the cost of buffering those pages in memory.

Calling cachedIn is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of PagingData emitted by Pager or any of its transformed derivatives, as reloading data from scratch on the same generation of PagingData is an unsupported operation.

Parameters
lifecycle: Lifecycle

The Lifecycle where the page cache will be kept alive.

cachedIn

fun <T : Any> LiveData<PagingData<T>>.cachedIn(viewModel: ViewModel): LiveData<PagingData<T>>

Operator which caches a LiveData of PagingData within the scope of a ViewModel.

cachedIn multicasts pages loaded and transformed by a PagingData, allowing multiple observers on the same instance of PagingData to receive the same events, avoiding redundant work, but comes at the cost of buffering those pages in memory.

Calling cachedIn is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of PagingData emitted by Pager or any of its transformed derivatives, as reloading data from scratch on the same generation of PagingData is an unsupported operation.

Parameters
viewModel: ViewModel

The ViewModel whose viewModelScope will dictate how long the page cache will be kept alive.

cachedIn

fun <T : Any> LiveData<PagingData<T>>.cachedIn(scope: CoroutineScope): LiveData<PagingData<T>>

Operator which caches a LiveData of PagingData within a CoroutineScope.

cachedIn multicasts pages loaded and transformed by a PagingData, allowing multiple observers on the same instance of PagingData to receive the same events, avoiding redundant work, but comes at the cost of buffering those pages in memory.

Calling cachedIn is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of PagingData emitted by Pager or any of its transformed derivatives, as reloading data from scratch on the same generation of PagingData is an unsupported operation.

Parameters
scope: CoroutineScope

The CoroutineScope where the page cache will be kept alive. Typically this would be a managed scope such as ViewModel.viewModelScope, which automatically cancels after the PagingData stream is no longer needed. Otherwise, the provided CoroutineScope must be manually cancelled to avoid memory leaks.

Public properties

liveData

val Pager<Key, Value>.liveDataLiveData<PagingData<Value>>

A LiveData of PagingData, which mirrors the stream provided by Pager.flow, but exposes it as a LiveData.

NOTE: Instances of PagingData emitted by this LiveData are not re-usable and cannot be submitted multiple times. This is especially relevant because LiveData will replays the latest value downstream. To ensure you get a new instance of PagingData for each downstream observer, you should use the cachedIn operator which multicasts the LiveData in a way that returns a new instance of PagingData with cached data pre-loaded.