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
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COConsider doing / basing your provider on something like [SQLiteContentProvider](http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android-apps/2.2_r1.1/com/android/providers/calendar/SQLiteContentProvider.java/?v=source) - it provides much of the underpinnings of a nice and functional provider based on SQLite - and if you do this - you should simply use bulkInsert or applyBatch to do your "mass inserts". The added benefit is that your bulk inserts will be performed in a transaction which speeds them up quite a lot.
      singulars
    2. CO@Jens many thanks for the pointer, this looks like exactly what I was after. Just a question about that: who has developed and released this code? From the header, it seems to be part of the The Android Open Source Project, but, if this is the case, why has it not been released by Google with the standard Android SDK?
      singulars
    3. COIt's part of the AOSP project but not published in the SDK - why I don't know since including it would've prevented a lot of people from writing crappy `ContentProvider` implementations of their own.
      singulars
 

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