PagingRx

Added in 3.0.0

public final class PagingRx


Summary

Public methods

static final @NonNull Flowable<@NonNull PagingData<@NonNull T>>

Operator which caches a Flowable of PagingData within a CoroutineScope.

static final @NonNull Observable<@NonNull PagingData<@NonNull T>>

Operator which caches an Observable of PagingData within a CoroutineScope.

static final @NonNull PagingData<@NonNull T>
<T extends Object> filter(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function1<@NonNull T, @NonNull Single<@NonNull Boolean>> predicate
)

Returns a PagingData containing only elements matching the given predicate.

static final @NonNull PagingData<@NonNull R>
<T extends Object, R extends Object> flatMap(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function1<@NonNull T, @NonNull Single<@NonNull Iterable<@NonNull R>>> transform
)

Returns a PagingData of all elements returned from applying the given transform to each element, as it is loaded.

static final @NonNull Flowable<@NonNull PagingData<@NonNull Value>>
getFlowable(@NonNull Pager<@NonNull Key, @NonNull Value> receiver)

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

static final @NonNull Observable<@NonNull PagingData<@NonNull Value>>
getObservable(@NonNull Pager<@NonNull Key, @NonNull Value> receiver)

An Observable of PagingData, which mirrors the stream provided by Pager.flow, but exposes it as an Observable.

static final @NonNull PagingData<@NonNull R>
<T extends R, R extends Object> insertSeparators(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function2<T, T, @NonNull Maybe<@NonNull R>> generator
)

Returns a PagingData containing each original element, with an optional separator generated by generator, given the elements before and after (or null, in boundary conditions).

static final @NonNull PagingData<@NonNull R>
<T extends Object, R extends Object> map(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function1<@NonNull T, @NonNull Single<@NonNull R>> transform
)

Returns a PagingData containing the result of applying the given transform to each element, as it is loaded.

Public methods

cachedIn

@ExperimentalCoroutinesApi
public static final @NonNull Flowable<@NonNull PagingData<@NonNull T>> <T extends Object> cachedIn(
    @NonNull Flowable<@NonNull PagingData<@NonNull T>> receiver,
    @NonNull CoroutineScope scope
)

Operator which caches a Flowable 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
@NonNull CoroutineScope scope

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.

cachedIn

@ExperimentalCoroutinesApi
public static final @NonNull Observable<@NonNull PagingData<@NonNull T>> <T extends Object> cachedIn(
    @NonNull Observable<@NonNull PagingData<@NonNull T>> receiver,
    @NonNull CoroutineScope scope
)

Operator which caches an Observable 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
@NonNull CoroutineScope scope

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 static final @NonNull PagingData<@NonNull T> <T extends Object> filter(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function1<@NonNull T, @NonNull Single<@NonNull Boolean>> predicate
)

Returns a PagingData containing only elements matching the given predicate.

flatMap

public static final @NonNull PagingData<@NonNull R> <T extends Object, R extends Object> flatMap(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function1<@NonNull T, @NonNull Single<@NonNull Iterable<@NonNull R>>> transform
)

Returns a PagingData of all elements returned from applying the given transform to each element, as it is loaded.

public static final @NonNull Flowable<@NonNull PagingData<@NonNull Value>> getFlowable(@NonNull Pager<@NonNull Key, @NonNull Value> receiver)

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

NOTE: Instances of PagingData emitted by this Flowable are not re-usable and cannot be submitted multiple times. This is especially relevant for transforms, which would replay 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 Flowable in a way that returns a new instance of PagingData with cached data pre-loaded.

public static final @NonNull Observable<@NonNull PagingData<@NonNull Value>> getObservable(@NonNull Pager<@NonNull Key, @NonNull Value> receiver)

An Observable of PagingData, which mirrors the stream provided by Pager.flow, but exposes it as an Observable.

NOTE: Instances of PagingData emitted by this Observable are not re-usable and cannot be submitted multiple times. This is especially relevant for transforms, which would replay 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 Observable in a way that returns a new instance of PagingData with cached data pre-loaded.

insertSeparators

public static final @NonNull PagingData<@NonNull R> <T extends R, R extends Object> insertSeparators(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function2<T, T, @NonNull Maybe<@NonNull R>> generator
)

Returns a PagingData containing each original element, with an optional separator generated by generator, given the elements before and after (or null, in boundary conditions).

Note that this transform is applied asynchronously, as pages are loaded. Potential separators between pages are only computed once both pages are loaded.

import androidx.paging.insertSeparators
import androidx.paging.insertSeparatorsAsync
import androidx.paging.rxjava2.insertSeparatorsAsync

/*
 * Create letter separators in an alphabetically sorted list.
 *
 * For example, if the input is:
 *     "apple", "apricot", "banana", "carrot"
 *
 * The operator would output:
 *     "A", "apple", "apricot", "B", "banana", "C", "carrot"
 */
pagingDataStream.map { pagingData ->
    // map outer stream, so we can perform transformations on each paging generation
    pagingData.insertSeparatorsAsync { before: String?, after: String? ->
        Maybe.fromCallable<String> {
            if (after != null && before?.first() != after.first()) {
                // separator - after is first item that starts with its first letter
                after.first().uppercaseChar().toString()
            } else {
                // no separator - either end of list, or first letters of before/after are the same
                null
            }
        }.subscribeOn(Schedulers.computation())
    }
}
import androidx.paging.insertSeparators
import androidx.paging.insertSeparatorsAsync
import androidx.paging.map
import androidx.paging.rxjava2.insertSeparatorsAsync

open class UiModel
data class ItemUiModel(val item: Item) : UiModel()
data class SeparatorUiModel(val char: Char) : UiModel()

/*
 * Create letter separators in an alphabetically sorted list of Items, with UiModel objects.
 *
 * For example, if the input is (each an `Item`):
 *     "apple", "apricot", "banana", "carrot"
 *
 * The operator would output a list of UiModels corresponding to:
 *     "A", "apple", "apricot", "B", "banana", "C", "carrot"
 */
pagingDataStream.map { pagingData ->
    // map outer stream, so we can perform transformations on each paging generation
    pagingData
        .map { item ->
            ItemUiModel(item) // convert items in stream to ItemUiModel
        }
        .insertSeparatorsAsync { before: ItemUiModel?, after: ItemUiModel? ->
            Maybe.fromCallable<UiModel> {
                if (after != null && before?.item?.label?.first() != after.item.label.first()) {
                    // separator - after is first item that starts with its first letter
                    SeparatorUiModel(after.item.label.first().uppercaseChar())
                } else {
                    // no separator - either end of list, or first letters of before/after are the same
                    null
                }
            }.subscribeOn(Schedulers.computation())
        }
}
public static final @NonNull PagingData<@NonNull R> <T extends Object, R extends Object> map(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function1<@NonNull T, @NonNull Single<@NonNull R>> transform
)

Returns a PagingData containing the result of applying the given transform to each element, as it is loaded.