# RxPagingSource

Artifact: [androidx.paging:paging-rxjava2](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/rxjava2/RxPagingSource.kt+class:androidx.paging.rxjava2.RxPagingSource) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/paging#3.0.0)

*** ** * ** ***


```
abstract class RxPagingSource<Key : Any, Value : Any> : PagingSource<Key, Value>
```

<br />

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.paging.PagingSource](https://developer.android.com/reference/kotlin/androidx/paging/PagingSource)\<Key, Value\> ||
|   | ↳ | [androidx.paging.rxjava2.RxPagingSource](https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/RxPagingSource) |

*** ** * ** ***

Rx-based compatibility wrapper around `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource`'s suspending APIs.

```kotlin
import androidx.paging.PagingState
import androidx.paging.rxjava2.RxPagingSource

/**
 * Sample RxPagingSource which loads `Item`s from network requests via Retrofit to a backend
 * service, which uses String tokens to load pages (each response has a next/previous token).
 */
class MyPagingSource(val myBackend: RxBackendService, val searchTerm: String) :
    RxPagingSource<String, Item>() {
    override fun loadSingle(params: LoadParams<String>): Single<LoadResult<String, Item>> {
        return myBackend
            // Single-based network load
            .searchUsers(searchTerm, params.key)
            .subscribeOn(Schedulers.io())
            .map<LoadResult<String, Item>> { result ->
                LoadResult.Page(
                    data = result.items,
                    prevKey = result.prev,
                    nextKey = result.next,
                )
            }
            .onErrorReturn { e ->
                when (e) {
                    // Retrofit calls that return the body type throw either IOException for
                    // network failures, or HttpException for any non-2xx HTTP status codes.
                    // This code reports all errors to the UI, but you can inspect/wrap the
                    // exceptions to provide more context.
                    is IOException -> LoadResult.Error(e)
                    is HttpException -> LoadResult.Error(e)
                    else -> throw e
                }
            }
    }

    override fun getRefreshKey(state: PagingState<String, Item>): String? {
        return state.anchorPosition?.let { state.closestItemToPosition(it)?.id }
    }
}
```

## Summary

| ### Public constructors |
|---|
| `<Key : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html, Value : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/RxPagingSource#RxPagingSource()()` |

| ### Public functions |
|---|---|
| `final suspend https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult<Key, Value>` | `https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/RxPagingSource#load(androidx.paging.PagingSource.LoadParams)(params: https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadParams<Key>)` |
| `abstract https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.html<https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult<Key, Value>>` | `https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/RxPagingSource#loadSingle(androidx.paging.PagingSource.LoadParams)(params: https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadParams<Key>)` Loading API for `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource`. |

### Inherited functions

From [androidx.paging.PagingSource](https://developer.android.com/reference/kotlin/androidx/paging/PagingSource) `abstract Key?` `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#getRefreshKey(androidx.paging.PagingState)(state: https://developer.android.com/reference/kotlin/androidx/paging/PagingState<Key, Value>)` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#invalidate()()` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#registerInvalidatedCallback(kotlin.Function0)(onInvalidatedCallback: () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#unregisterInvalidatedCallback(kotlin.Function0)(onInvalidatedCallback: () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)`

### Inherited properties

From [androidx.paging.PagingSource](https://developer.android.com/reference/kotlin/androidx/paging/PagingSource) `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#invalid()` `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#jumpingSupported()` `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#keyReuseSupported()`

## Public constructors

### RxPagingSource

```
<Key : Any, Value : Any> RxPagingSource()
```

## Public functions

### load

```
final suspend fun load(params: PagingSource.LoadParams<Key>): PagingSource.LoadResult<Key, Value>
```

### loadSingle

Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/paging#3.0.0)

```
abstract fun loadSingle(params: PagingSource.LoadParams<Key>): Single<PagingSource.LoadResult<Key, Value>>
```

Loading API for `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource`.

Implement this method to trigger your async load (e.g. from database or network).