{% setvar book_path %}/reference/kotlin/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

PagingDataFutures

{% setvar page_path %}androidx/paging/PagingDataFutures.html{% endsetvar %} {% setvar can_switch %}1{% endsetvar %} {% include "reference/_kotlin_switcher2.md" %}

class PagingDataFutures


Summary

Public functions

@<ERROR CLASS> PagingData<T>
<T : Any> PagingData<T>.filter(
    predicate: <ERROR CLASS><T, Boolean>,
    executor: Executor
)

Returns a PagingData containing only elements matching the given predicate.

@<ERROR CLASS> PagingData<R>
<T : Any, R : Any> PagingData<T>.flatMap(
    transform: <ERROR CLASS><T, Iterable<R>>,
    executor: Executor
)

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

@<ERROR CLASS> PagingData<R>
<T : R, R : Any> PagingData<T>.insertSeparators(
    generator: <ERROR CLASS><AdjacentItems<T>, R?>,
    executor: Executor
)

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).

@<ERROR CLASS> PagingData<R>
<T : Any, R : Any> PagingData<T>.map(
    transform: <ERROR CLASS><T, R>,
    executor: Executor
)

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

Public functions

filter

@<ERROR CLASS>
fun <T : Any> PagingData<T>.filter(
    predicate: <ERROR CLASS><T, Boolean>,
    executor: Executor
): @<ERROR CLASS> PagingData<T>

Returns a PagingData containing only elements matching the given predicate.

Parameters
predicate: <ERROR CLASS><T, Boolean>

AsyncFunction returning false for unmatched items which should be filtered.

executor: Executor

Executor to run the AsyncFunction in.

flatMap

@<ERROR CLASS>
fun <T : Any, R : Any> PagingData<T>.flatMap(
    transform: <ERROR CLASS><T, Iterable<R>>,
    executor: Executor
): @<ERROR CLASS> PagingData<R>

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

Parameters
transform: <ERROR CLASS><T, Iterable<R>>

AsyncFunction to transform an item of type T a list of items of type R.

executor: Executor

Executor to run the AsyncFunction in.

insertSeparators

@<ERROR CLASS>
fun <T : R, R : Any> PagingData<T>.insertSeparators(
    generator: <ERROR CLASS><AdjacentItems<T>, R?>,
    executor: Executor
): @<ERROR CLASS> PagingData<R>

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.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(
        AsyncFunction<AdjacentItems<String>, String?> {
            Futures.submit(
                Callable<String?> {
                    val (before, after) = it!!
                    if (after != null && before?.first() != after.first()) {
                        // separator - after is first item that starts with its first letter
                        after.first().toUpperCase().toString()
                    } else {
                        // no separator - either end of list, or first letters of before/after are the same
                        null
                    }
                },
                executor
            )
        },
        executor
    )
}
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(
            AsyncFunction<AdjacentItems<ItemUiModel>, UiModel?> {
                Futures.submit(
                    Callable<UiModel> {
                        val (before, after) = it!!
                        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().toUpperCase())
                        } else {
                            // no separator - either end of list, or first letters of before/after are the same
                            null
                        }
                    },
                    executor
                )
            },
            executor
        )
}
Parameters
generator: <ERROR CLASS><AdjacentItems<T>, R?>

AsyncFunction used to generate separator between two AdjacentItems or the header or footer if either AdjacentItems.before or AdjacentItems.after is null.

executor: Executor

Executor to run the AsyncFunction in.

map

@<ERROR CLASS>
fun <T : Any, R : Any> PagingData<T>.map(
    transform: <ERROR CLASS><T, R>,
    executor: Executor
): @<ERROR CLASS> PagingData<R>

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

Parameters
transform: <ERROR CLASS><T, R>

AsyncFunction to transform an item of type T to R.

executor: Executor

Executor to run the AsyncFunction in.