Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid ContentProvider calls bursts of setNotificationUri() to CursorAdapter when many rows are inserted with a batch operation
    text
    copied!<p>I have a custom <code>ContentProvider</code> which manages the access to a SQLite database. To load the content of a database table in a <code>ListFragment</code>, I use the <code>LoaderManager</code> with a <code>CursorLoader</code> and a <code>CursorAdapter</code>:</p> <pre class="lang-java prettyprint-override"><code>public class MyListFragment extends ListFragment implements LoaderCallbacks&lt;Cursor&gt; { // ... CursorAdapter mAdapter; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mAdapter = new CursorAdapter(getActivity(), null, 0); setListAdapter(mAdapter); getLoaderManager().initLoader(LOADER_ID, null, this); } public Loader&lt;Cursor&gt; onCreateLoader(int id, Bundle args) { return new CursorLoader(getActivity(), CONTENT_URI, PROJECTION, null, null, null); } public void onLoadFinished(Loader&lt;Cursor&gt; loader, Cursor c) { mAdapter.swapCursor(c); } public void onLoaderReset(Loader&lt;Cursor&gt; loader) { mAdapter.swapCursor(null); } } </code></pre> <p>The SQLite database gets updated by a background task which fetches a number of items from a web service and inserts these items into the database through <code>ContentProvider</code> batch operations (<code>ContentResolver#applyBatch()</code>).</p> <p>Even if this is a batch operation, <code>ContentProvider#insert()</code> gets called for each row is inserted into the database and, in the current implementation, the <code>ContentProvider</code> calls <code>setNotificationUri()</code> for each insert command. </p> <p>The result is that the <code>CursorAdapter</code> receives bursts of notifications, resulting in the UI being updated too often with consequent annoying flickering effect.</p> <p>Ideally, when a batch operation is in progress, there should be a way to notify <code>ContentObserver</code> only at the end of any batch operation and not with each insert command.</p> <p>Does anybody know if this is possible? Please note I can change the <code>ContentProvider</code> implementation and override any of its methods.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload