blob: c8ca4c5777f558802ad029db630b342ddb0e69c3 [file] [log] [blame]
<html devsite="true">
<head>
<title>DataSource</title>
{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %}
{% include "_shared/_reference-head-tags.html" %}
</head>
<body>
<h1>DataSource</h1>
{% setvar page_path %}androidx/paging/DataSource.html{% endsetvar %}
{% setvar can_switch %}1{% endsetvar %}
{% include "reference/_java_switcher2.md" %}
<p>
<pre>public abstract class DataSource&lt;Key&nbsp;extends&nbsp;<a href="/reference/java/lang/Object.html">Object</a>,&nbsp;Value&nbsp;extends&nbsp;<a href="/reference/java/lang/Object.html">Object</a>&gt;</pre>
</p>
<div class="devsite-table-wrapper"><devsite-expandable><span class="expand-control jd-sumtable-subclasses">Known direct subclasses
<div class="showalways" id="subclasses-direct"><a href="/reference/androidx/paging/ItemKeyedDataSource.html">ItemKeyedDataSource</a>, <a href="/reference/androidx/paging/PageKeyedDataSource.html">PageKeyedDataSource</a>, <a href="/reference/androidx/paging/PositionalDataSource.html">PositionalDataSource</a></div>
</span>
<div id="subclasses-direct-summary">
<div class="devsite-table-wrapper">
<table class="responsive">
<tbody>
<tr>
<td><code><a href="/reference/androidx/paging/ItemKeyedDataSource.html">ItemKeyedDataSource</a></code></td>
<td width="100%">
<p><strong>This class is deprecated.</strong> ItemKeyedDataSource is deprecated and has been replaced by PagingSource</p>
</td>
</tr>
<tr>
<td><code><a href="/reference/androidx/paging/PageKeyedDataSource.html">PageKeyedDataSource</a></code></td>
<td width="100%">
<p><strong>This class is deprecated.</strong> PageKeyedDataSource is deprecated and has been replaced by PagingSource</p>
</td>
</tr>
<tr>
<td><code><a href="/reference/androidx/paging/PositionalDataSource.html">PositionalDataSource</a></code></td>
<td width="100%">
<p><strong>This class is deprecated.</strong> PositionalDataSource is deprecated and has been replaced by PagingSource</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</devsite-expandable> </div>
<hr>
<p>Base class for loading pages of snapshot data into a <code><a href="/reference/androidx/paging/PagedList.html">PagedList</a></code>.</p>
<p>DataSource is queried to load pages of content into a <code><a href="/reference/androidx/paging/PagedList.html">PagedList</a></code>. A PagedList can grow as it loads more data, but the data loaded cannot be updated. If the underlying data set is modified, a new PagedList / DataSource pair must be created to represent the new data.</p>
<h3> Loading Pages</h3>
<p>PagedList queries data from its DataSource in response to loading hints. PagedListAdapter calls <code><a href="/reference/androidx/paging/PagedList.html#loadAround(kotlin.Int)">PagedList.loadAround</a></code> to load content as the user scrolls in a RecyclerView.</p>
<p>To control how and when a PagedList queries data from its DataSource, see <code><a href="/reference/androidx/paging/PagedList.Config.html">PagedList.Config</a></code>. The Config object defines things like load sizes and prefetch distance.</p>
<h3> Updating Paged Data</h3>
<p>A PagedList / DataSource pair are a snapshot of the data set. A new pair of PagedList / DataSource must be created if an update occurs, such as a reorder, insert, delete, or content update occurs. A DataSource 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/androidx/paging/DataSource.html#invalidate()">invalidate</a></code>. Then a new PagedList / DataSource pair would be created to load data from the new state of the Database query.</p>
<p>To page in data that doesn't update, you can create a single DataSource, and pass it to a single PagedList. For example, loading from network when the network's paging API doesn't provide updates.</p>
<p>To page in data from a source that does provide updates, you can create a <code><a href="/reference/androidx/paging/DataSource.Factory.html">DataSource.Factory</a></code>, where each DataSource created is invalidated when an update to the data set occurs that makes the current snapshot invalid. For example, when paging a query from the Database, and the table being queried inserts or removes items. You can also use a DataSource.Factory to provide multiple versions of network-paged lists. If reloading all content (e.g. in response to an action like swipe-to-refresh) is required to get a new version of data, you can connect an explicit refresh signal to call <code><a href="/reference/androidx/paging/DataSource.html#invalidate()">invalidate</a></code> on the current <code><a href="/reference/androidx/paging/DataSource.html">DataSource</a></code>.</p>
<p>If you have more granular update signals, such as a network API signaling an update to a single item in the list, it's recommended to load data from network into memory. Then present that data to the PagedList via a DataSource that wraps an in-memory snapshot. Each time the in-memory copy changes, invalidate the previous DataSource, and a new one wrapping the new state of the snapshot can be created.</p>
<h3> Implementing a DataSource</h3>
<p>To implement, extend one of the subclasses: <code><a href="/reference/androidx/paging/PageKeyedDataSource.html">PageKeyedDataSource</a></code>, <code><a href="/reference/androidx/paging/ItemKeyedDataSource.html">ItemKeyedDataSource</a></code>, or <code><a href="/reference/androidx/paging/PositionalDataSource.html">PositionalDataSource</a></code>.</p>
<p>Use <code><a href="/reference/androidx/paging/PageKeyedDataSource.html">PageKeyedDataSource</a></code> if pages you load embed keys for loading adjacent pages. For example a network response that returns some items, and a next/previous page links.</p>
<p>Use <code><a href="/reference/androidx/paging/ItemKeyedDataSource.html">ItemKeyedDataSource</a></code> if you need to use data from item <code>N-1</code> to load item <code>N</code>. For example, if requesting the backend for the next comments in the list requires the ID or timestamp of the most recent loaded comment, or if querying the next users from a name-sorted database query requires the name and unique ID of the previous.</p>
<p>Use <code><a href="/reference/androidx/paging/PositionalDataSource.html">PositionalDataSource</a></code> if you can load pages of a requested size at arbitrary positions, and provide a fixed item count. PositionalDataSource supports querying pages at arbitrary positions, so can provide data to PagedLists in arbitrary order. Note that PositionalDataSource is required to respect page size for efficient tiling. If you want to override page size (e.g. when network page size constraints are only known at runtime), use one of the other DataSource classes.</p>
<p>Because a <code>null</code> item indicates a placeholder in <code><a href="/reference/androidx/paging/PagedList.html">PagedList</a></code>, DataSource may not return <code>null</code> items in lists that it loads. This is so that users of the PagedList can differentiate unloaded placeholder items from content that has been paged in.</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2">Parameters</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>&lt;Key&nbsp;extends&nbsp;<a href="/reference/java/lang/Object.html">Object</a>&gt;</code></td>
<td width="100%">
<p>Unique identifier for item loaded from DataSource. Often an integer to represent position in data set. Note - this is distinct from e.g. Room's `&lt;Value&gt; Value type loaded by the DataSource.</p>
</td>
</tr>
</tbody>
</table>
</div>
<h2>Summary</h2>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2"><h3>Nested types</h3></th>
</tr>
</thead>
<tbody>
<tr>
<td><code><a href="/reference/androidx/paging/DataSource.Factory.html">DataSource.Factory</a></code></td>
<td width="100%">
<p>Factory for DataSources.</p>
</td>
</tr>
<tr>
<td><code><a href="/reference/androidx/paging/DataSource.InvalidatedCallback.html">DataSource.InvalidatedCallback</a></code></td>
<td width="100%">
<p>Invalidation callback for <code><a href="/reference/androidx/paging/DataSource.html">DataSource</a></code>.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2"><h3>Public fields</h3></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>boolean</code></td>
<td width="100%">
<div><code><a href="/reference/androidx/paging/DataSource.html#isInvalid()">isInvalid</a></code></div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2"><h3>Public methods</h3></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>void</code></td>
<td width="100%">
<div><code><a href="/reference/androidx/paging/DataSource.html#addInvalidatedCallback(androidx.paging.DataSource.InvalidatedCallback)">addInvalidatedCallback</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="/reference/androidx/paging/DataSource.InvalidatedCallback.html">DataSource.InvalidatedCallback</a>&nbsp;onInvalidatedCallback<br>)</code></div>
<p>Add a callback to invoke when the DataSource is first invalidated.</p>
</td>
</tr>
<tr>
<td><code>void</code></td>
<td width="100%">
<div><code><a href="/reference/androidx/paging/DataSource.html#invalidate()">invalidate</a>()</code></div>
<p>Signal the data source to stop loading, and notify its callback.</p>
</td>
</tr>
<tr>
<td><code><a href="/reference/androidx/paging/DataSource.html">DataSource</a>&lt;Key,&nbsp;ToValue&gt;</code></td>
<td width="100%">
<div><code>&lt;ToValue&nbsp;extends&nbsp;<a href="/reference/java/lang/Object.html">Object</a>&gt; <a href="/reference/androidx/paging/DataSource.html#map()">map</a>(<a href="/reference/[JVM root]/&lt;ERROR CLASS&gt;.html">&lt;ERROR CLASS&gt;</a>&lt;Value,&nbsp;ToValue&gt;&nbsp;function)</code></div>
<p>Applies the given function to each value emitted by the DataSource.</p>
</td>
</tr>
<tr>
<td><code><a href="/reference/androidx/paging/DataSource.html">DataSource</a>&lt;Key,&nbsp;ToValue&gt;</code></td>
<td width="100%">
<div><code>&lt;ToValue&nbsp;extends&nbsp;<a href="/reference/java/lang/Object.html">Object</a>&gt; <a href="/reference/androidx/paging/DataSource.html#mapByPage()">mapByPage</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="/reference/[JVM root]/&lt;ERROR CLASS&gt;.html">&lt;ERROR CLASS&gt;</a>&lt;<a href="/reference/java/util/List.html">List</a>&lt;Value&gt;,&nbsp;<a href="/reference/java/util/List.html">List</a>&lt;ToValue&gt;&gt;&nbsp;function<br>)</code></div>
<p>Applies the given function to each value emitted by the DataSource.</p>
</td>
</tr>
<tr>
<td><code>void</code></td>
<td width="100%">
<div><code><a href="/reference/androidx/paging/DataSource.html#removeInvalidatedCallback(androidx.paging.DataSource.InvalidatedCallback)">removeInvalidatedCallback</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="/reference/androidx/paging/DataSource.InvalidatedCallback.html">DataSource.InvalidatedCallback</a>&nbsp;onInvalidatedCallback<br>)</code></div>
<p>Remove a previously added invalidate callback.</p>
</td>
</tr>
</tbody>
</table>
</div>
<h2>Public fields</h2>
<div><a name="getIsInvalid()"></a><a name="setIsInvalid()"></a><a name="getIsInvalid--"></a><a name="setIsInvalid--"></a>
<h3 class="api-name" id="isInvalid()">isInvalid</h3>
<pre class="api-signature no-pretty-print">public&nbsp;boolean&nbsp;<a href="/reference/androidx/paging/DataSource.html#isInvalid()">isInvalid</a></pre>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2">Returns</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>boolean</code></td>
<td width="100%">
<p><code>true</code> if the data source is invalid, and can no longer be queried for data.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>Public methods</h2>
<div><a name="addInvalidatedCallback-androidx.paging.DataSource.InvalidatedCallback-"></a>
<h3 class="api-name" id="addInvalidatedCallback(androidx.paging.DataSource.InvalidatedCallback)">addInvalidatedCallback</h3>
<pre class="api-signature no-pretty-print">@<a href="/reference/[JVM root]/&lt;ERROR CLASS&gt;.html">&lt;ERROR CLASS&gt;</a><br>public&nbsp;void&nbsp;<a href="/reference/androidx/paging/DataSource.html#addInvalidatedCallback(androidx.paging.DataSource.InvalidatedCallback)">addInvalidatedCallback</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="/reference/androidx/paging/DataSource.InvalidatedCallback.html">DataSource.InvalidatedCallback</a>&nbsp;onInvalidatedCallback<br>)</pre>
<p>Add a callback to invoke when the DataSource is first invalidated.</p>
<p>Once invalidated, a data source will not become valid again.</p>
<p>A data source will only invoke its callbacks once - the first time <code><a href="/reference/androidx/paging/DataSource.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><a href="/reference/androidx/paging/DataSource.InvalidatedCallback.html">DataSource.InvalidatedCallback</a>&nbsp;onInvalidatedCallback</code></td>
<td width="100%">
<p>The callback, will be invoked on thread that invalidates the <code><a href="/reference/androidx/paging/DataSource.html">DataSource</a></code>.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div><a name="invalidate--"></a>
<h3 class="api-name" id="invalidate()">invalidate</h3>
<pre class="api-signature no-pretty-print">@<a href="/reference/[JVM root]/&lt;ERROR CLASS&gt;.html">&lt;ERROR CLASS&gt;</a><br>public&nbsp;void&nbsp;<a href="/reference/androidx/paging/DataSource.html#invalidate()">invalidate</a>()</pre>
<p>Signal the data source to stop loading, and notify its callback.</p>
<p>If invalidate has already been called, this method does nothing.</p>
</div>
<div><a name="map--"></a>
<h3 class="api-name" id="map()">map</h3>
<pre class="api-signature no-pretty-print">public&nbsp;<a href="/reference/androidx/paging/DataSource.html">DataSource</a>&lt;Key,&nbsp;ToValue&gt;&nbsp;&lt;ToValue&nbsp;extends&nbsp;<a href="/reference/java/lang/Object.html">Object</a>&gt; <a href="/reference/androidx/paging/DataSource.html#map()">map</a>(<a href="/reference/[JVM root]/&lt;ERROR CLASS&gt;.html">&lt;ERROR CLASS&gt;</a>&lt;Value,&nbsp;ToValue&gt;&nbsp;function)</pre>
<p>Applies the given function to each value emitted by the DataSource.</p>
<p>Same as <code><a href="/reference/androidx/paging/DataSource.html#mapByPage()">mapByPage</a></code>, but operates on individual items.</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2">Returns</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><a href="/reference/androidx/paging/DataSource.html">DataSource</a>&lt;Key,&nbsp;ToValue&gt;</code></td>
<td width="100%">
<p>A new DataSource, which transforms items using the given function.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2">Parameters</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>&lt;ToValue&nbsp;extends&nbsp;<a href="/reference/java/lang/Object.html">Object</a>&gt;</code></td>
<td width="100%">
<p>Type of items produced by the new DataSource, from the passed function.</p>
</td>
</tr>
<tr>
<td><code><a href="/reference/[JVM root]/&lt;ERROR CLASS&gt;.html">&lt;ERROR CLASS&gt;</a>&lt;Value,&nbsp;ToValue&gt;&nbsp;function</code></td>
<td width="100%">
<p>Function that runs on each loaded item, returning items of a potentially new type.</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/androidx/paging/DataSource.html#mapByPage()">mapByPage</a></code></td>
<td width="100%"></td>
</tr>
<tr>
<td><code><a href="/reference/androidx/paging/DataSource.Factory.html#map()">map</a></code></td>
<td width="100%"></td>
</tr>
<tr>
<td><code><a href="/reference/androidx/paging/DataSource.Factory.html#mapByPage()">mapByPage</a></code></td>
<td width="100%"></td>
</tr>
</tbody>
</table>
</div>
</div>
<div><a name="mapByPage--"></a>
<h3 class="api-name" id="mapByPage()">mapByPage</h3>
<pre class="api-signature no-pretty-print">public&nbsp;<a href="/reference/androidx/paging/DataSource.html">DataSource</a>&lt;Key,&nbsp;ToValue&gt;&nbsp;&lt;ToValue&nbsp;extends&nbsp;<a href="/reference/java/lang/Object.html">Object</a>&gt; <a href="/reference/androidx/paging/DataSource.html#mapByPage()">mapByPage</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="/reference/[JVM root]/&lt;ERROR CLASS&gt;.html">&lt;ERROR CLASS&gt;</a>&lt;<a href="/reference/java/util/List.html">List</a>&lt;Value&gt;,&nbsp;<a href="/reference/java/util/List.html">List</a>&lt;ToValue&gt;&gt;&nbsp;function<br>)</pre>
<p>Applies the given function to each value emitted by the DataSource.</p>
<p>Same as <code><a href="/reference/androidx/paging/DataSource.html#map()">map</a></code>, but allows for batch conversions.</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2">Returns</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><a href="/reference/androidx/paging/DataSource.html">DataSource</a>&lt;Key,&nbsp;ToValue&gt;</code></td>
<td width="100%">
<p>A new DataSource, which transforms items using the given function.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="2">Parameters</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>&lt;ToValue&nbsp;extends&nbsp;<a href="/reference/java/lang/Object.html">Object</a>&gt;</code></td>
<td width="100%">
<p>Type of items produced by the new DataSource, from the passed function.</p>
</td>
</tr>
<tr>
<td><code><a href="/reference/[JVM root]/&lt;ERROR CLASS&gt;.html">&lt;ERROR CLASS&gt;</a>&lt;<a href="/reference/java/util/List.html">List</a>&lt;Value&gt;,&nbsp;<a href="/reference/java/util/List.html">List</a>&lt;ToValue&gt;&gt;&nbsp;function</code></td>
<td width="100%">
<p>Function that runs on each loaded page, returning items of a potentially new type.</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/androidx/paging/DataSource.html#map()">map</a></code></td>
<td width="100%"></td>
</tr>
<tr>
<td><code><a href="/reference/androidx/paging/DataSource.Factory.html#map()">map</a></code></td>
<td width="100%"></td>
</tr>
<tr>
<td><code><a href="/reference/androidx/paging/DataSource.Factory.html#mapByPage()">mapByPage</a></code></td>
<td width="100%"></td>
</tr>
</tbody>
</table>
</div>
</div>
<div><a name="removeInvalidatedCallback-androidx.paging.DataSource.InvalidatedCallback-"></a>
<h3 class="api-name" id="removeInvalidatedCallback(androidx.paging.DataSource.InvalidatedCallback)">removeInvalidatedCallback</h3>
<pre class="api-signature no-pretty-print">@<a href="/reference/[JVM root]/&lt;ERROR CLASS&gt;.html">&lt;ERROR CLASS&gt;</a><br>public&nbsp;void&nbsp;<a href="/reference/androidx/paging/DataSource.html#removeInvalidatedCallback(androidx.paging.DataSource.InvalidatedCallback)">removeInvalidatedCallback</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="/reference/androidx/paging/DataSource.InvalidatedCallback.html">DataSource.InvalidatedCallback</a>&nbsp;onInvalidatedCallback<br>)</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><a href="/reference/androidx/paging/DataSource.InvalidatedCallback.html">DataSource.InvalidatedCallback</a>&nbsp;onInvalidatedCallback</code></td>
<td width="100%">
<p>The previously added callback.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>