blob: 08fec8b522839193113e28bc83a6f7c3c60712d6 [file] [log] [blame]
Alex Saveaue60da312020-09-09 23:34:43 +00001<html devsite="true">
2 <head>
3 <title>PagingSource</title>
4{% setvar book_path %}/reference/kotlin/androidx/_book.yaml{% endsetvar %}
5{% include "_shared/_reference-head-tags.html" %}
6 </head>
7 <body>
8 <h1>PagingSource</h1>
Andrea Falcone4be8b872020-10-15 12:34:30 -04009 <p>
Paweł Marks4724d6f2021-12-22 17:11:51 +010010 <pre>abstract class PagingSource&lt;Key&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>,&nbsp;Value&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>&gt;</pre>
Andrea Falcone4be8b872020-10-15 12:34:30 -040011 </p>
Alex Saveau20bf0662020-09-23 03:45:51 +000012 <div class="devsite-table-wrapper"><devsite-expandable><span class="expand-control jd-sumtable-subclasses">Known direct subclasses
13 <div class="showalways" id="subclasses-direct"><a href="/reference/kotlin/androidx/paging/ListenableFuturePagingSource.html">ListenableFuturePagingSource</a>, <a href="/reference/kotlin/androidx/paging/rxjava2/RxPagingSource.html">RxPagingSource</a>, <a href="/reference/kotlin/androidx/paging/rxjava3/RxPagingSource.html">RxPagingSource</a></div>
14 </span>
15 <div id="subclasses-direct-summary">
16 <div class="devsite-table-wrapper">
17 <table class="responsive">
18 <tbody>
19 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -040020 <td width="40%"><code><a href="/reference/kotlin/androidx/paging/ListenableFuturePagingSource.html">ListenableFuturePagingSource</a></code></td>
21 <td>
Paweł Marks4724d6f2021-12-22 17:11:51 +010022 <p><code><a href="https://guava.dev/releases/18.0/api/docs/package-list/com/google/common/util/concurrent/ListenableFuture.html">ListenableFuture</a></code>-based compatibility wrapper around <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>'s suspending APIs.</p>
Alex Saveau20bf0662020-09-23 03:45:51 +000023 </td>
24 </tr>
25 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -040026 <td width="40%"><code><a href="/reference/kotlin/androidx/paging/rxjava2/RxPagingSource.html">RxPagingSource</a></code></td>
27 <td>
Alex Saveau20bf0662020-09-23 03:45:51 +000028 <p>Rx-based compatibility wrapper around <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>'s suspending APIs.</p>
29 </td>
30 </tr>
31 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -040032 <td width="40%"><code><a href="/reference/kotlin/androidx/paging/rxjava3/RxPagingSource.html">RxPagingSource</a></code></td>
33 <td>
Alex Saveau20bf0662020-09-23 03:45:51 +000034 <p>Rx-based compatibility wrapper around <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>'s suspending APIs.</p>
35 </td>
36 </tr>
37 </tbody>
38 </table>
39 </div>
40 </div>
41</devsite-expandable> </div>
Alex Saveaue60da312020-09-09 23:34:43 +000042 <hr>
Alex Saveaufe9fe472020-09-10 01:41:31 +000043 <p>Base class for an abstraction of pageable static data from some source, where loading pages of data is typically an expensive operation. Some examples of common <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>s might be from network or from a database.</p>
44 <p>An instance of a <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> is used to load pages of data for an instance of <code><a href="/reference/kotlin/androidx/paging/PagingData.html">PagingData</a></code>.</p>
45 <p>A <code><a href="/reference/kotlin/androidx/paging/PagingData.html">PagingData</a></code> can grow as it loads more data, but the data loaded cannot be updated. If the underlying data set is modified, a new <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> / <code><a href="/reference/kotlin/androidx/paging/PagingData.html">PagingData</a></code> pair must be created to represent an updated snapshot of the data.</p>
46 <h3> Loading Pages</h3>
47 <p><code><a href="/reference/kotlin/androidx/paging/PagingData.html">PagingData</a></code> queries data from its <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> in response to loading hints generated as the user scrolls in a <code>RecyclerView</code>.</p>
Alex Saveau7ffa75d2020-09-19 05:45:27 +000048 <p>To control how and when a <code><a href="/reference/kotlin/androidx/paging/PagingData.html">PagingData</a></code> queries data from its <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>, see <code><a href="/reference/kotlin/androidx/paging/PagingConfig.html">PagingConfig</a></code>, which defines behavior such as <code><a href="/reference/kotlin/androidx/paging/PagingConfig.html#pageSize()">PagingConfig.pageSize</a></code> and <code><a href="/reference/kotlin/androidx/paging/PagingConfig.html#prefetchDistance()">PagingConfig.prefetchDistance</a></code>.</p>
Alex Saveaufe9fe472020-09-10 01:41:31 +000049 <h3> Updating Data</h3>
50 <p>A <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> / <code><a href="/reference/kotlin/androidx/paging/PagingData.html">PagingData</a></code> pair is a snapshot of the data set. A new <code><a href="/reference/kotlin/androidx/paging/PagingData.html">PagingData</a></code> / <code><a href="/reference/kotlin/androidx/paging/PagingData.html">PagingData</a></code> must be created if an update occurs, such as a reorder, insert, delete, or content update occurs. A <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> must detect that it cannot continue loading its snapshot (for instance, when Database query notices a table being invalidated), and call <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#invalidate()">invalidate</a></code>. Then a new <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> / <code><a href="/reference/kotlin/androidx/paging/PagingData.html">PagingData</a></code> pair would be created to represent data from the new state of the database query.</p>
51 <h3> Presenting Data to UI</h3>
52 <p>To present data loaded by a <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> to a <code>RecyclerView</code>, create an instance of <code><a href="/reference/kotlin/androidx/paging/Pager.html">Pager</a></code>, which provides a stream of <code><a href="/reference/kotlin/androidx/paging/PagingData.html">PagingData</a></code> that you may collect from and submit to a <code><a href="/reference/kotlin/androidx/paging/PagingDataAdapter.html">PagingDataAdapter</a></code>.</p>
Owen Grayeb256132021-01-27 13:49:50 -050053 <pre class="prettyprint">/**
Owen Gray327bd5c2021-03-23 10:21:18 -040054 * Sample page-keyed PagingSource, which uses Int page number to load pages.
Owen Grayeb256132021-01-27 13:49:50 -050055 *
56 * Loads Items from network requests via Retrofit to a backend service.
57 *
58 * Note that the key type is Int, since we're using page number to load a page.
59 */
60class MyPagingSource(
61 val myBackend: MyBackendService
62) : PagingSource&lt;Int, Item&gt;() {
63 override suspend fun load(params: LoadParams&lt;Int&gt;): LoadResult&lt;Int, Item&gt; {
64
65 // Retrofit calls that return the body type throw either IOException for network
66 // failures, or HttpException for any non-2xx HTTP status codes. This code reports all
67 // errors to the UI, but you can inspect/wrap the exceptions to provide more context.
68 return try {
69 // Key may be null during a refresh, if no explicit key is passed into Pager
70 // construction. Use 0 as default, because our API is indexed started at index 0
71 val pageNumber = params.key ?: 0
72
73 // Suspending network load via Retrofit. This doesn't need to be wrapped in a
74 // withContext(Dispatcher.IO) { ... } block since Retrofit's Coroutine
75 // CallAdapter dispatches on a worker thread.
76 val response = myBackend.searchItems(pageNumber)
77
78 // Since 0 is the lowest page number, return null to signify no more pages should
79 // be loaded before it.
80 val prevKey = if (pageNumber &gt; 0) pageNumber - 1 else null
81
82 // This API defines that it's out of data when a page returns empty. When out of
83 // data, we return `null` to signify no more pages should be loaded
84 val nextKey = if (response.items.isNotEmpty()) pageNumber + 1 else null
85 LoadResult.Page(
86 data = response.items,
87 prevKey = prevKey,
88 nextKey = nextKey
89 )
90 } catch (e: IOException) {
91 LoadResult.Error(e)
92 } catch (e: HttpException) {
93 LoadResult.Error(e)
94 }
95 }
96
97 override fun getRefreshKey(state: PagingState&lt;Int, Item&gt;): Int? {
98 return state.anchorPosition?.let {
99 state.closestPageToPosition(it)?.prevKey?.plus(1)
100 ?: state.closestPageToPosition(it)?.nextKey?.minus(1)
101 }
102 }
103}</pre>
Owen Gray327bd5c2021-03-23 10:21:18 -0400104 <pre class="prettyprint">/**
105 * Sample item-keyed [PagingSource], which uses String tokens to load pages.
106 *
107 * Loads Items from network requests via Retrofit to a backend service.
108 */
109class MyPagingSource(
110 val myBackend: MyBackendService
111) : PagingSource&lt;String, Item&gt;() {
112 override suspend fun load(params: LoadParams&lt;String&gt;): LoadResult&lt;String, Item&gt; {
113 // Retrofit calls that return the body type throw either IOException for network
114 // failures, or HttpException for any non-2xx HTTP status codes. This code reports all
115 // errors to the UI, but you can inspect/wrap the exceptions to provide more context.
116 return try {
117 // Suspending network load via Retrofit. This doesn't need to be wrapped in a
118 // withContext(Dispatcher.IO) { ... } block since Retrofit's Coroutine
119 // CallAdapter dispatches on a worker thread.
120 val response = myBackend.searchItems(params.key)
121 LoadResult.Page(
122 data = response.items,
123 prevKey = response.prev,
124 nextKey = response.next
125 )
126 } catch (e: IOException) {
127 LoadResult.Error(e)
128 } catch (e: HttpException) {
129 LoadResult.Error(e)
130 }
131 }
132
133 override fun getRefreshKey(state: PagingState&lt;String, Item&gt;): String? {
134 return state.anchorPosition?.let { state.closestItemToPosition(it)?.id }
135 }
136}</pre>
Alex Saveaue60da312020-09-09 23:34:43 +0000137 <div class="devsite-table-wrapper">
138 <table class="responsive">
139 <thead>
140 <tr>
141 <th colspan="2">Parameters</th>
142 </tr>
143 </thead>
144 <tbody>
145 <tr>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100146 <td width="40%"><code>&lt;Key&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>&gt;</code></td>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400147 <td>
Andrea Falcone3adcc282020-12-04 09:31:29 -0500148 <p>Type of key which define what data to load. E.g. <code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html">Int</a></code> to represent either a page number or item position, or <code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a></code> if your network uses Strings as next tokens returned with each response.</p>
Alex Saveaue60da312020-09-09 23:34:43 +0000149 </td>
150 </tr>
151 <tr>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100152 <td width="40%"><code>&lt;Value&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>&gt;</code></td>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400153 <td>
Alex Saveaufe9fe472020-09-10 01:41:31 +0000154 <p>Type of data loaded in by this <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>. E.g., the type of data that will be passed to a <code><a href="/reference/kotlin/androidx/paging/PagingDataAdapter.html">PagingDataAdapter</a></code> to be displayed in a <code>RecyclerView</code>.</p>
Alex Saveaue60da312020-09-09 23:34:43 +0000155 </td>
156 </tr>
157 </tbody>
158 </table>
159 </div>
160 <div class="devsite-table-wrapper">
161 <table class="responsive">
162 <thead>
163 <tr>
164 <th colspan="2">See also</th>
165 </tr>
166 </thead>
167 <tbody>
168 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400169 <td width="40%"><code><a href="/reference/kotlin/androidx/paging/Pager.html">Pager</a></code></td>
170 <td></td>
Alex Saveaue60da312020-09-09 23:34:43 +0000171 </tr>
172 </tbody>
173 </table>
174 </div>
175 <h2>Summary</h2>
Alex Saveaue60da312020-09-09 23:34:43 +0000176 <div class="devsite-table-wrapper">
177 <table class="responsive">
178 <thead>
179 <tr>
Alex Saveau66428452020-09-17 22:44:07 +0000180 <th colspan="2"><h3>Nested types</h3></th>
181 </tr>
182 </thead>
183 <tbody>
184 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400185 <td width="40%"><code><a href="/reference/kotlin/androidx/paging/PagingSource.LoadParams.html">PagingSource.LoadParams</a></code></td>
186 <td>
Alex Saveau3555c952020-09-18 03:05:17 +0000187 <p>Params for a load request on a <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> from <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">PagingSource.load</a></code>.</p>
Alex Saveau66428452020-09-17 22:44:07 +0000188 </td>
189 </tr>
190 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400191 <td width="40%"><code><a href="/reference/kotlin/androidx/paging/PagingSource.LoadParams.Append.html">PagingSource.LoadParams.Append</a></code></td>
192 <td>
Alex Saveau3555c952020-09-18 03:05:17 +0000193 <p>Params to load a page of data from a <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> via <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">PagingSource.load</a></code> to be appended to the end of the list.</p>
Alex Saveau66428452020-09-17 22:44:07 +0000194 </td>
195 </tr>
196 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400197 <td width="40%"><code><a href="/reference/kotlin/androidx/paging/PagingSource.LoadParams.Prepend.html">PagingSource.LoadParams.Prepend</a></code></td>
198 <td>
Alex Saveau3555c952020-09-18 03:05:17 +0000199 <p>Params to load a page of data from a <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> via <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">PagingSource.load</a></code> to be prepended to the start of the list.</p>
Alex Saveau66428452020-09-17 22:44:07 +0000200 </td>
201 </tr>
202 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400203 <td width="40%"><code><a href="/reference/kotlin/androidx/paging/PagingSource.LoadParams.Refresh.html">PagingSource.LoadParams.Refresh</a></code></td>
204 <td>
Alex Saveau3555c952020-09-18 03:05:17 +0000205 <p>Params for an initial load request on a <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> from <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">PagingSource.load</a></code> or a refresh triggered by <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#invalidate()">invalidate</a></code>.</p>
Alex Saveau66428452020-09-17 22:44:07 +0000206 </td>
207 </tr>
208 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400209 <td width="40%"><code><a href="/reference/kotlin/androidx/paging/PagingSource.LoadResult.html">PagingSource.LoadResult</a></code></td>
210 <td>
Alex Saveau3555c952020-09-18 03:05:17 +0000211 <p>Result of a load request from <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">PagingSource.load</a></code>.</p>
Alex Saveau66428452020-09-17 22:44:07 +0000212 </td>
213 </tr>
214 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400215 <td width="40%"><code><a href="/reference/kotlin/androidx/paging/PagingSource.LoadResult.Error.html">PagingSource.LoadResult.Error</a></code></td>
216 <td>
Alex Saveau3555c952020-09-18 03:05:17 +0000217 <p>Error result object for <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">PagingSource.load</a></code>.</p>
Alex Saveau66428452020-09-17 22:44:07 +0000218 </td>
219 </tr>
220 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400221 <td width="40%"><code><a href="/reference/kotlin/androidx/paging/PagingSource.LoadResult.Page.html">PagingSource.LoadResult.Page</a></code></td>
222 <td>
Alex Saveau3555c952020-09-18 03:05:17 +0000223 <p>Success result object for <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">PagingSource.load</a></code>.</p>
Alex Saveau66428452020-09-17 22:44:07 +0000224 </td>
225 </tr>
226 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400227 <td width="40%"><code><a href="/reference/kotlin/androidx/paging/PagingSource.LoadResult.Page.Companion.html">PagingSource.LoadResult.Page.Companion</a></code></td>
228 <td></td>
Alex Saveau66428452020-09-17 22:44:07 +0000229 </tr>
230 </tbody>
231 </table>
232 </div>
233 <div class="devsite-table-wrapper">
234 <table class="responsive">
235 <thead>
236 <tr>
Alex Saveauf393e9a2020-09-17 21:15:59 +0000237 <th colspan="2"><h3>Public constructors</h3></th>
238 </tr>
239 </thead>
240 <tbody>
241 <tr>
242 <td>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100243 <div><code>&lt;Key&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>,&nbsp;Value&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>&gt; <a href="/reference/kotlin/androidx/paging/PagingSource.html#PagingSource()">PagingSource</a>()</code></div>
Alex Saveauf393e9a2020-09-17 21:15:59 +0000244 </td>
245 </tr>
246 </tbody>
247 </table>
248 </div>
249 <div class="devsite-table-wrapper">
250 <table class="responsive">
251 <thead>
252 <tr>
Alex Saveaue60da312020-09-09 23:34:43 +0000253 <th colspan="2"><h3>Public functions</h3></th>
254 </tr>
255 </thead>
256 <tbody>
257 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400258 <td width="40%"><code>abstract Key?</code></td>
259 <td>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100260 <div><code><a href="/reference/kotlin/androidx/paging/PagingSource.html#getRefreshKey(androidx.paging.PagingState)">getRefreshKey</a>(state:&nbsp;<a href="/reference/kotlin/androidx/paging/PagingState.html">PagingState</a>&lt;Key,&nbsp;Value&gt;)</code></div>
Owen Gray327bd5c2021-03-23 10:21:18 -0400261 <p>Provide a <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">Key</a></code> used for the initial <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">load</a></code> for the next <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> due to invalidation of this <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>.</p>
Alex Saveaue60da312020-09-09 23:34:43 +0000262 </td>
263 </tr>
264 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400265 <td width="40%"><code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a></code></td>
266 <td>
Alex Saveaue60da312020-09-09 23:34:43 +0000267 <div><code><a href="/reference/kotlin/androidx/paging/PagingSource.html#invalidate()">invalidate</a>()</code></div>
Alex Saveaufe9fe472020-09-10 01:41:31 +0000268 <p>Signal the <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> to stop loading.</p>
Alex Saveaue60da312020-09-09 23:34:43 +0000269 </td>
270 </tr>
271 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400272 <td width="40%"><code>abstract&nbsp;suspend <a href="/reference/kotlin/androidx/paging/PagingSource.LoadResult.html">PagingSource.LoadResult</a>&lt;Key,&nbsp;Value&gt;</code></td>
273 <td>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100274 <div><code><a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">load</a>(params:&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.LoadParams.html">PagingSource.LoadParams</a>&lt;Key&gt;)</code></div>
Alex Saveaufe9fe472020-09-10 01:41:31 +0000275 <p>Loading API for <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>.</p>
Alex Saveaue60da312020-09-09 23:34:43 +0000276 </td>
277 </tr>
278 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400279 <td width="40%"><code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a></code></td>
280 <td>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100281 <div><code><a href="/reference/kotlin/androidx/paging/PagingSource.html#registerInvalidatedCallback(kotlin.Function0)">registerInvalidatedCallback</a>(onInvalidatedCallback:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a>)</code></div>
Alex Saveaufe9fe472020-09-10 01:41:31 +0000282 <p>Add a callback to invoke when the <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> is first invalidated.</p>
Alex Saveaue60da312020-09-09 23:34:43 +0000283 </td>
284 </tr>
285 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400286 <td width="40%"><code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a></code></td>
287 <td>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100288 <div><code><a href="/reference/kotlin/androidx/paging/PagingSource.html#unregisterInvalidatedCallback(kotlin.Function0)">unregisterInvalidatedCallback</a>(onInvalidatedCallback:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a>)</code></div>
Alex Saveaufe9fe472020-09-10 01:41:31 +0000289 <p>Remove a previously added invalidate callback.</p>
Alex Saveaue60da312020-09-09 23:34:43 +0000290 </td>
291 </tr>
292 </tbody>
293 </table>
294 </div>
Fred Sladkey375e2ba2021-05-26 20:13:00 +0000295 <div class="devsite-table-wrapper">
296 <table class="responsive">
297 <thead>
298 <tr>
299 <th colspan="2"><h3>Public properties</h3></th>
300 </tr>
301 </thead>
302 <tbody>
303 <tr>
304 <td width="40%"><code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html">Boolean</a></code></td>
305 <td>
306 <div><code><a href="/reference/kotlin/androidx/paging/PagingSource.html#invalid()">invalid</a></code></div>
307 <p>Whether this <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> has been invalidated, which should happen when the data this <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> represents changes since it was first instantiated.</p>
308 </td>
309 </tr>
310 <tr>
311 <td width="40%"><code>open <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html">Boolean</a></code></td>
312 <td>
313 <div><code><a href="/reference/kotlin/androidx/paging/PagingSource.html#jumpingSupported()">jumpingSupported</a></code></div>
314 <p><code>true</code> if this <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> supports jumping, <code>false</code> otherwise.</p>
315 </td>
316 </tr>
317 <tr>
318 <td width="40%"><code>open <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html">Boolean</a></code></td>
319 <td>
320 <div><code><a href="/reference/kotlin/androidx/paging/PagingSource.html#keyReuseSupported()">keyReuseSupported</a></code></div>
321 <p><code>true</code> if this <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> expects to re-use keys to load distinct pages without a call to <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#invalidate()">invalidate</a></code>, <code>false</code> otherwise.</p>
322 </td>
323 </tr>
324 </tbody>
325 </table>
Alex Saveau65f73172020-09-09 22:46:21 +0000326 </div>
Alex Saveauf393e9a2020-09-17 21:15:59 +0000327 <h2>Public constructors</h2>
Fred Sladkey86625e32021-05-24 19:28:27 -0400328 <div><a name="PagingSource--"></a><a name="pagingsource"></a>
Alex Saveauf393e9a2020-09-17 21:15:59 +0000329 <h3 class="api-name" id="PagingSource()">PagingSource</h3>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100330 <pre class="api-signature no-pretty-print">&lt;Key&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>,&nbsp;Value&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>&gt; <a href="/reference/kotlin/androidx/paging/PagingSource.html#PagingSource()">PagingSource</a>()</pre>
Alex Saveauf393e9a2020-09-17 21:15:59 +0000331 <div class="devsite-table-wrapper">
332 <table class="responsive">
333 <thead>
334 <tr>
335 <th colspan="2">Parameters</th>
336 </tr>
337 </thead>
338 <tbody>
339 <tr>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100340 <td width="40%"><code>&lt;Key&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>&gt;</code></td>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400341 <td>
Andrea Falcone3adcc282020-12-04 09:31:29 -0500342 <p>Type of key which define what data to load. E.g. <code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html">Int</a></code> to represent either a page number or item position, or <code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a></code> if your network uses Strings as next tokens returned with each response.</p>
Alex Saveauf393e9a2020-09-17 21:15:59 +0000343 </td>
344 </tr>
345 <tr>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100346 <td width="40%"><code>&lt;Value&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>&gt;</code></td>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400347 <td>
Alex Saveauf393e9a2020-09-17 21:15:59 +0000348 <p>Type of data loaded in by this <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>. E.g., the type of data that will be passed to a <code><a href="/reference/kotlin/androidx/paging/PagingDataAdapter.html">PagingDataAdapter</a></code> to be displayed in a <code>RecyclerView</code>.</p>
349 </td>
350 </tr>
351 </tbody>
352 </table>
353 </div>
354 </div>
Alex Saveaue60da312020-09-09 23:34:43 +0000355 <h2>Public functions</h2>
Fred Sladkey86625e32021-05-24 19:28:27 -0400356 <div><a name="getRefreshKey-androidx.paging.PagingState-"></a><a name="getrefreshkey"></a>
Alex Saveaue60da312020-09-09 23:34:43 +0000357 <h3 class="api-name" id="getRefreshKey(androidx.paging.PagingState)">getRefreshKey</h3>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100358 <pre class="api-signature no-pretty-print">abstract&nbsp;fun&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.html#getRefreshKey(androidx.paging.PagingState)">getRefreshKey</a>(state:&nbsp;<a href="/reference/kotlin/androidx/paging/PagingState.html">PagingState</a>&lt;Key,&nbsp;Value&gt;):&nbsp;Key?</pre>
Owen Gray327bd5c2021-03-23 10:21:18 -0400359 <p>Provide a <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">Key</a></code> used for the initial <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">load</a></code> for the next <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> due to invalidation of this <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>. The <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">Key</a></code> is provided to <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">load</a></code> via <code><a href="/reference/kotlin/androidx/paging/PagingSource.LoadParams.html#key()">LoadParams.key</a></code>.</p>
360 <p>The <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">Key</a></code> returned by this method should cause <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">load</a></code> to load enough items to fill the viewport around the last accessed position, allowing the next generation to transparently animate in. The last accessed position can be retrieved via <code><a href="/reference/kotlin/androidx/paging/PagingState.html#anchorPosition()">state.anchorPosition</a></code>, which is typically the top-most or bottom-most item in the viewport due to access being triggered by binding items as they scroll into view.</p>
361 <p>For example, if items are loaded based on integer position keys, you can return <code><a href="/reference/kotlin/androidx/paging/PagingState.html#anchorPosition()">state.anchorPosition</a></code>.</p>
362 <p>Alternately, if items contain a key used to load, get the key from the item in the page at index <code><a href="/reference/kotlin/androidx/paging/PagingState.html#anchorPosition()">state.anchorPosition</a></code>.</p>
363 <div class="devsite-table-wrapper">
364 <table class="responsive">
365 <thead>
366 <tr>
367 <th colspan="2">Parameters</th>
368 </tr>
369 </thead>
370 <tbody>
371 <tr>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100372 <td width="40%"><code>state:&nbsp;<a href="/reference/kotlin/androidx/paging/PagingState.html">PagingState</a>&lt;Key,&nbsp;Value&gt;</code></td>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400373 <td>
Owen Gray327bd5c2021-03-23 10:21:18 -0400374 <p><code><a href="/reference/kotlin/androidx/paging/PagingState.html">PagingState</a></code> of the currently fetched data, which includes the most recently accessed position in the list via <code><a href="/reference/kotlin/androidx/paging/PagingState.html#anchorPosition()">PagingState.anchorPosition</a></code>.</p>
375 </td>
376 </tr>
377 </tbody>
378 </table>
379 </div>
380 <div class="devsite-table-wrapper">
381 <table class="responsive">
382 <thead>
383 <tr>
384 <th colspan="2">Returns</th>
385 </tr>
386 </thead>
387 <tbody>
388 <tr>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400389 <td width="40%"><code>Key?</code></td>
390 <td>
Owen Gray327bd5c2021-03-23 10:21:18 -0400391 <p><code><a href="/reference/kotlin/androidx/paging/PagingSource.html">Key</a></code> passed to <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">load</a></code> after invalidation used for initial load of the next generation. The <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">Key</a></code> returned by <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#getRefreshKey(androidx.paging.PagingState)">getRefreshKey</a></code> should load pages centered around user's current viewport. If the correct <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">Key</a></code> cannot be determined, <code>null</code> can be returned to allow <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">load</a></code> decide what default key to use.</p>
392 </td>
393 </tr>
394 </tbody>
395 </table>
396 </div>
Alex Saveaue60da312020-09-09 23:34:43 +0000397 </div>
Fred Sladkey86625e32021-05-24 19:28:27 -0400398 <div><a name="invalidate--"></a><a name="invalidate"></a>
Alex Saveaue60da312020-09-09 23:34:43 +0000399 <h3 class="api-name" id="invalidate()">invalidate</h3>
Owen Gray327bd5c2021-03-23 10:21:18 -0400400 <pre class="api-signature no-pretty-print">fun&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.html#invalidate()">invalidate</a>():&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a></pre>
Alex Saveaufe9fe472020-09-10 01:41:31 +0000401 <p>Signal the <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> to stop loading.</p>
402 <p>This method is idempotent. i.e., If <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#invalidate()">invalidate</a></code> has already been called, subsequent calls to this method should have no effect.</p>
Alex Saveaue60da312020-09-09 23:34:43 +0000403 </div>
Fred Sladkey86625e32021-05-24 19:28:27 -0400404 <div><a name="load-androidx.paging.PagingSource.LoadParams-"></a><a name="load"></a>
Alex Saveaue60da312020-09-09 23:34:43 +0000405 <h3 class="api-name" id="load(androidx.paging.PagingSource.LoadParams)">load</h3>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100406 <pre class="api-signature no-pretty-print">abstract&nbsp;suspend&nbsp;fun&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.html#load(androidx.paging.PagingSource.LoadParams)">load</a>(params:&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.LoadParams.html">PagingSource.LoadParams</a>&lt;Key&gt;):&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.LoadResult.html">PagingSource.LoadResult</a>&lt;Key,&nbsp;Value&gt;</pre>
Alex Saveaufe9fe472020-09-10 01:41:31 +0000407 <p>Loading API for <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>.</p>
408 <p>Implement this method to trigger your async load (e.g. from database or network).</p>
Alex Saveaue60da312020-09-09 23:34:43 +0000409 </div>
Fred Sladkey86625e32021-05-24 19:28:27 -0400410 <div><a name="registerInvalidatedCallback-kotlin.Function0-"></a><a name="registerinvalidatedcallback"></a>
Alex Saveaue60da312020-09-09 23:34:43 +0000411 <h3 class="api-name" id="registerInvalidatedCallback(kotlin.Function0)">registerInvalidatedCallback</h3>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100412 <pre class="api-signature no-pretty-print">fun&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.html#registerInvalidatedCallback(kotlin.Function0)">registerInvalidatedCallback</a>(onInvalidatedCallback:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a>):&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a></pre>
Alex Saveaufe9fe472020-09-10 01:41:31 +0000413 <p>Add a callback to invoke when the <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> is first invalidated.</p>
414 <p>Once invalidated, a <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> will not become valid again.</p>
415 <p>A <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> will only invoke its callbacks once - the first time <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#invalidate()">invalidate</a></code> is called, on that thread.</p>
Alex Saveaue60da312020-09-09 23:34:43 +0000416 <div class="devsite-table-wrapper">
417 <table class="responsive">
418 <thead>
419 <tr>
420 <th colspan="2">Parameters</th>
421 </tr>
422 </thead>
423 <tbody>
424 <tr>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100425 <td width="40%"><code>onInvalidatedCallback:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a></code></td>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400426 <td>
Alex Saveaue60da312020-09-09 23:34:43 +0000427 <p>The callback that will be invoked on thread that invalidates the <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>.</p>
428 </td>
429 </tr>
430 </tbody>
431 </table>
432 </div>
433 </div>
Fred Sladkey86625e32021-05-24 19:28:27 -0400434 <div><a name="unregisterInvalidatedCallback-kotlin.Function0-"></a><a name="unregisterinvalidatedcallback"></a>
Alex Saveaue60da312020-09-09 23:34:43 +0000435 <h3 class="api-name" id="unregisterInvalidatedCallback(kotlin.Function0)">unregisterInvalidatedCallback</h3>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100436 <pre class="api-signature no-pretty-print">fun&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.html#unregisterInvalidatedCallback(kotlin.Function0)">unregisterInvalidatedCallback</a>(onInvalidatedCallback:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a>):&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a></pre>
Alex Saveaufe9fe472020-09-10 01:41:31 +0000437 <p>Remove a previously added invalidate callback.</p>
Alex Saveaue60da312020-09-09 23:34:43 +0000438 <div class="devsite-table-wrapper">
439 <table class="responsive">
440 <thead>
441 <tr>
442 <th colspan="2">Parameters</th>
443 </tr>
444 </thead>
445 <tbody>
446 <tr>
Paweł Marks4724d6f2021-12-22 17:11:51 +0100447 <td width="40%"><code>onInvalidatedCallback:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a></code></td>
Andrea Falconed77cf9d2021-04-26 17:34:56 -0400448 <td>
Alex Saveaue60da312020-09-09 23:34:43 +0000449 <p>The previously added callback.</p>
450 </td>
451 </tr>
452 </tbody>
453 </table>
454 </div>
455 </div>
Fred Sladkey375e2ba2021-05-26 20:13:00 +0000456 <h2>Public properties</h2>
457 <div><a name="getInvalid()"></a><a name="setInvalid()"></a><a name="getInvalid--"></a><a name="setInvalid--"></a>
458 <h3 class="api-name" id="invalid()">invalid</h3>
459 <pre class="api-signature no-pretty-print">val&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.html#invalid()">invalid</a>:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html">Boolean</a></pre>
460 <p>Whether this <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> has been invalidated, which should happen when the data this <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> represents changes since it was first instantiated.</p>
461 </div>
462 <div><a name="getJumpingSupported()"></a><a name="setJumpingSupported()"></a><a name="getJumpingSupported--"></a><a name="setJumpingSupported--"></a>
463 <h3 class="api-name" id="jumpingSupported()">jumpingSupported</h3>
464 <pre class="api-signature no-pretty-print">open&nbsp;val&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.html#jumpingSupported()">jumpingSupported</a>:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html">Boolean</a></pre>
465 <p><code>true</code> if this <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> supports jumping, <code>false</code> otherwise.</p>
466 <p>Override this to <code>true</code> if pseudo-fast scrolling via jumps is supported.</p>
Fred Sladkey6b33bca2021-06-01 17:39:06 +0000467 <p>A jump occurs when a <code>RecyclerView</code> scrolls through a number of placeholders defined by <code><a href="/reference/kotlin/androidx/paging/PagingConfig.html#jumpThreshold()">PagingConfig.jumpThreshold</a></code> and triggers a load with <code><a href="/reference/kotlin/androidx/paging/LoadType.html#REFRESH">LoadType</a></code>.</p>
Fred Sladkey375e2ba2021-05-26 20:13:00 +0000468 <p><code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>s that support jumps should override <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#getRefreshKey(androidx.paging.PagingState)">getRefreshKey</a></code> to return a <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">Key</a></code> that would load data fulfilling the viewport given a user's current <code><a href="/reference/kotlin/androidx/paging/PagingState.html#anchorPosition()">PagingState.anchorPosition</a></code>.</p>
469 <div class="devsite-table-wrapper">
470 <table class="responsive">
471 <thead>
472 <tr>
473 <th colspan="2">See also</th>
474 </tr>
475 </thead>
476 <tbody>
477 <tr>
478 <td width="40%"><code><a href="/reference/kotlin/androidx/paging/PagingConfig.html#jumpThreshold()">jumpThreshold</a></code></td>
479 <td></td>
480 </tr>
481 </tbody>
482 </table>
483 </div>
484 </div>
485 <div><a name="getKeyReuseSupported()"></a><a name="setKeyReuseSupported()"></a><a name="getKeyReuseSupported--"></a><a name="setKeyReuseSupported--"></a>
486 <h3 class="api-name" id="keyReuseSupported()">keyReuseSupported</h3>
487 <pre class="api-signature no-pretty-print">open&nbsp;val&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.html#keyReuseSupported()">keyReuseSupported</a>:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html">Boolean</a></pre>
488 <p><code>true</code> if this <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> expects to re-use keys to load distinct pages without a call to <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#invalidate()">invalidate</a></code>, <code>false</code> otherwise.</p>
489 </div>
Alex Saveaue60da312020-09-09 23:34:43 +0000490 </body>
491</html>