Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable notifications on a ContentProvider URI

I'm looking for a way to suspend notifications on a given ContentProvider's Uri. The use case is:

  1. An Activity is bound to a CursorAdapter through a CursorLoader.
  2. A Service may do a lot of batch, single-row updates on a ContentProvider.
  3. The CursorLoader will reload its content on every row update, as the ContentProvider notifies listeners by ContentResolver#notifyChange.

Since I cannot edit the ContentProvider, and I have no control over the batch queries execution, is there a way to suspend notifications on a Uri (in the executing Service) until all of the ContentProvider-managed queries have been executed? I need this in order to avoid the flickering caused by the continuous requerying of the CursorLoader.

like image 917
frapontillo Avatar asked Apr 08 '13 06:04

frapontillo


People also ask

What is ContentProvider and ContentResolver default ContentProvider of Android?

A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network.

What is content URI in Android?

A content URI is a URI that identifies data in a provider. Content URIs include the symbolic name of the entire provider (its authority) and a name that points to a table (a path). When you call a client method to access a table in a provider, the content URI for the table is one of the arguments.

What is a ContentProvider and what is it typically used for?

A content provider can be used to manage access to a variety of data storage sources, including both structured data, such as a SQLite relational database, or unstructured data such as image files. For more information on the types of storage available on Android, see Storage options, as well as Designing data storage.

How can an application activate ContentProvider?

Create a class in the same directory where the that MainActivity file resides and this class must extend the ContentProvider base class. To access the content, define a content provider URI address. Create a database to store the application data. Implement the six abstract methods of ContentProvider class.


1 Answers

You cannot disable this mechanism in your Service. But you should try to batch them by using ContentProviderOperations.

I've written an introductory post about ContentProviderOperations and two additional posts covering the methods withYieldAllowed() and withBackReference() respectively.

Especially the latter one should be of interest for what you've described here.

With ContentProviderOperations you can batch multiple updates and inserts. If you then call applyBatch() on your ContentResolver object the ContentProvider executes them all at once.

Now I've never used Nicolas Klein's generator but since he is a very, very proficient Android developer and works at Google, I bet that the generated code makes use of transactions and calls notifyChange() only once for the complete batch at the end.

Exactly what you need.

like image 149
Wolfram Rittmeyer Avatar answered Oct 15 '22 20:10

Wolfram Rittmeyer