blob: 6b92f5f6088145caf47d7d7279d80a29ed10f4d4 [file] [log] [blame]
<html devsite="true">
<head>
<title>PagingSource</title>
{% setvar book_path %}/reference/kotlin/androidx/_book.yaml{% endsetvar %}
{% include "_shared/_reference-head-tags.html" %}
</head>
<body>
<h1>PagingSource</h1>
{% setvar page_path %}androidx/paging/PagingSource.html{% endsetvar %}
{% setvar can_switch %}1{% endsetvar %}
{% include "reference/_kotlin_switcher2.md" %}
<p>TODO(b/166518424) class signature</p>
<p>TODO(b/166518951) inheritance hierarchy</p>
<p>TODO(b/166518636) direct subclasses</p>
<p>TODO(b/166518636) indirect subclasses</p>
<hr>
<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>
<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>
<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>
<h3> Loading Pages</h3>
<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>
<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()">pageSize</a></code> and <code><a href="/reference/kotlin/androidx/paging/PagingConfig.html#prefetchDistance()">prefetchDistance</a></code>.</p>
<h3> Updating Data</h3>
<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>
<h3> Presenting Data to UI</h3>
<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>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2">Parameters</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Key</code></td>
<td width="100%">
<p>Type of key which define what data to load. E.g. <code><a href="/reference/kotlin/kotlin/Int.html">Int</a></code> to represent either a page number or item position, or <code><a href="/reference/kotlin/kotlin/String.html">String</a></code> if your network uses Strings as next tokens returned with each response.</p>
</td>
</tr>
<tr>
<td><code>Value</code></td>
<td width="100%">
<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>
</td>
</tr>
</tbody>
</table>
</div>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2">See also</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><a href="/reference/kotlin/androidx/paging/Pager.html">Pager</a></code></td>
<td width="100%">
<p></p>
</td>
</tr>
</tbody>
</table>
</div>
<h2>Summary</h2>
<p>Nested *</p>
<p>Enum values</p>
<p>Public constructors</p>
<p>Protected constructors</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2"><h3>Public properties</h3></th>
</tr>
</thead>
<tbody>
<tr>
<td><code><a href="/reference/kotlin/kotlin/Boolean.html">Boolean</a></code></td>
<td width="100%">
<div><code><a href="/reference/kotlin/androidx/paging/PagingSource.html#invalid()">invalid</a></code></div>
<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>
</td>
</tr>
<tr>
<td><code>open <a href="/reference/kotlin/kotlin/Boolean.html">Boolean</a></code></td>
<td width="100%">
<div><code><a href="/reference/kotlin/androidx/paging/PagingSource.html#jumpingSupported()">jumpingSupported</a></code></div>
<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>
</td>
</tr>
<tr>
<td><code>open <a href="/reference/kotlin/kotlin/Boolean.html">Boolean</a></code></td>
<td width="100%">
<div><code><a href="/reference/kotlin/androidx/paging/PagingSource.html#keyReuseSupported()">keyReuseSupported</a></code></div>
<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>
</td>
</tr>
</tbody>
</table>
</div>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2"><h3>Public functions</h3></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>open Key</code></td>
<td width="100%">
<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>
<p>Request a refresh key given the current <code><a href="/reference/kotlin/androidx/paging/PagingState.html">PagingState</a></code> of the associated <code><a href="/reference/kotlin/androidx/paging/PagingData.html">PagingData</a></code> used to present loaded data from this <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>.</p>
</td>
</tr>
<tr>
<td><code>open <a href="/reference/kotlin/kotlin/Unit.html">Unit</a></code></td>
<td width="100%">
<div><code><a href="/reference/kotlin/androidx/paging/PagingSource.html#invalidate()">invalidate</a>()</code></div>
<p>Signal the <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> to stop loading.</p>
</td>
</tr>
<tr>
<td><code>suspend&nbsp;abstract <a href="/reference/kotlin/androidx/paging/PagingSource.LoadResult.html">PagingSource.LoadResult</a>&lt;Key,&nbsp;Value&gt;</code></td>
<td width="100%">
<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>
<p>Loading API for <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>.</p>
</td>
</tr>
<tr>
<td><code><a href="/reference/kotlin/kotlin/Unit.html">Unit</a></code></td>
<td width="100%">
<div><code><a href="/reference/kotlin/androidx/paging/PagingSource.html#registerInvalidatedCallback(kotlin.Function0)">registerInvalidatedCallback</a>(onInvalidatedCallback:&nbsp;()&nbsp;-&gt;&nbsp;<a href="/reference/kotlin/kotlin/Unit.html">Unit</a>)</code></div>
<p>Add a callback to invoke when the <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> is first invalidated.</p>
</td>
</tr>
<tr>
<td><code><a href="/reference/kotlin/kotlin/Unit.html">Unit</a></code></td>
<td width="100%">
<div><code><a href="/reference/kotlin/androidx/paging/PagingSource.html#unregisterInvalidatedCallback(kotlin.Function0)">unregisterInvalidatedCallback</a>(onInvalidatedCallback:&nbsp;()&nbsp;-&gt;&nbsp;<a href="/reference/kotlin/kotlin/Unit.html">Unit</a>)</code></div>
<p>Remove a previously added invalidate callback.</p>
</td>
</tr>
</tbody>
</table>
</div>
<h2>Public properties</h2>
<div><a name="getInvalid()"></a><a name="setInvalid()"></a><a name="getInvalid--"></a><a name="setInvalid--"></a>
<h3 class="api-name" id="invalid()">invalid</h3>
<pre class="api-signature no-pretty-print">val&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.html#invalid()">invalid</a>:&nbsp;<a href="/reference/kotlin/kotlin/Boolean.html">Boolean</a></pre>
<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>
</div>
<div><a name="getJumpingSupported()"></a><a name="setJumpingSupported()"></a><a name="getJumpingSupported--"></a><a name="setJumpingSupported--"></a>
<h3 class="api-name" id="jumpingSupported()">jumpingSupported</h3>
<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="/reference/kotlin/kotlin/Boolean.html">Boolean</a></pre>
<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>
<p>Override this to <code>true</code> if pseudo-fast scrolling via jumps is supported.</p>
<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()">jumpThreshold</a></code> and triggers a load with <code><a href="/reference/kotlin/androidx/paging/LoadType.REFRESH.html">LoadType.REFRESH</a></code>.</p>
<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">PagingSource</a></code> that would load data fulfilling the viewport given a user's current <code><a href="/reference/kotlin/androidx/paging/PagingState.html#anchorPosition()">anchorPosition</a></code>.</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2">See also</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><a href="/reference/kotlin/androidx/paging/PagingConfig.html#jumpThreshold()">jumpThreshold</a></code></td>
<td width="100%">
<p></p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div><a name="getKeyReuseSupported()"></a><a name="setKeyReuseSupported()"></a><a name="getKeyReuseSupported--"></a><a name="setKeyReuseSupported--"></a>
<h3 class="api-name" id="keyReuseSupported()">keyReuseSupported</h3>
<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="/reference/kotlin/kotlin/Boolean.html">Boolean</a></pre>
<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>
</div>
<h2>Public functions</h2>
<div><a name="getRefreshKey-androidx.paging.PagingState-"></a>
<h3 class="api-name" id="getRefreshKey(androidx.paging.PagingState)">getRefreshKey</h3>
<pre class="api-signature no-pretty-print">@<a href="/reference/kotlin/androidx/paging/ExperimentalPagingApi.html">ExperimentalPagingApi</a><br>open&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>
<p>Request a refresh key given the current <code><a href="/reference/kotlin/androidx/paging/PagingState.html">PagingState</a></code> of the associated <code><a href="/reference/kotlin/androidx/paging/PagingData.html">PagingData</a></code> used to present loaded data from this <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>.</p>
<p>The <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> returned by this method is used to populate the <code><a href="/reference/kotlin/androidx/paging/PagingSource.LoadParams.html#key()">key</a></code> for load requests of type <code><a href="/reference/kotlin/androidx/paging/LoadType.REFRESH.html">LoadType.REFRESH</a></code>.</p>
<p>For example, if items are loaded based on position, and keys are positions, <code><a href="/reference/kotlin/androidx/paging/PagingSource.html#getRefreshKey(androidx.paging.PagingState)">getRefreshKey</a></code> should return the position of the item.</p>
<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()">anchorPosition</a></code>.</p>
<p>If this operation cannot be supported (generally, because keys cannot be reused across refresh) return <code>null</code> - this is the default behavior.</p>
<p>Note: This method is guaranteed to only be called if the initial load succeeds and the list of loaded pages is not empty. In the case where a refresh is triggered before the initial load succeeds or it errors out, the initial key passed to <code><a href="/reference/kotlin/androidx/paging/Pager.html">Pager</a></code> will be used.</p>
</div>
<div><a name="invalidate--"></a>
<h3 class="api-name" id="invalidate()">invalidate</h3>
<pre class="api-signature no-pretty-print">open&nbsp;fun&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.html#invalidate()">invalidate</a>():&nbsp;<a href="/reference/kotlin/kotlin/Unit.html">Unit</a></pre>
<p>Signal the <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> to stop loading.</p>
<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>
</div>
<div><a name="load-androidx.paging.PagingSource.LoadParams-"></a>
<h3 class="api-name" id="load(androidx.paging.PagingSource.LoadParams)">load</h3>
<pre class="api-signature no-pretty-print">suspend&nbsp;abstract&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>
<p>Loading API for <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code>.</p>
<p>Implement this method to trigger your async load (e.g. from database or network).</p>
</div>
<div><a name="registerInvalidatedCallback-kotlin.Function0-"></a>
<h3 class="api-name" id="registerInvalidatedCallback(kotlin.Function0)">registerInvalidatedCallback</h3>
<pre class="api-signature no-pretty-print">fun&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.html#registerInvalidatedCallback(kotlin.Function0)">registerInvalidatedCallback</a>(onInvalidatedCallback:&nbsp;()&nbsp;-&gt;&nbsp;<a href="/reference/kotlin/kotlin/Unit.html">Unit</a>):&nbsp;<a href="/reference/kotlin/kotlin/Unit.html">Unit</a></pre>
<p>Add a callback to invoke when the <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> is first invalidated.</p>
<p>Once invalidated, a <code><a href="/reference/kotlin/androidx/paging/PagingSource.html">PagingSource</a></code> will not become valid again.</p>
<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>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2">Parameters</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>onInvalidatedCallback</code></td>
<td width="100%">
<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>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div><a name="unregisterInvalidatedCallback-kotlin.Function0-"></a>
<h3 class="api-name" id="unregisterInvalidatedCallback(kotlin.Function0)">unregisterInvalidatedCallback</h3>
<pre class="api-signature no-pretty-print">fun&nbsp;<a href="/reference/kotlin/androidx/paging/PagingSource.html#unregisterInvalidatedCallback(kotlin.Function0)">unregisterInvalidatedCallback</a>(onInvalidatedCallback:&nbsp;()&nbsp;-&gt;&nbsp;<a href="/reference/kotlin/kotlin/Unit.html">Unit</a>):&nbsp;<a href="/reference/kotlin/kotlin/Unit.html">Unit</a></pre>
<p>Remove a previously added invalidate callback.</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2">Parameters</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>onInvalidatedCallback</code></td>
<td width="100%">
<p>The previously added callback.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>