I'm looking for a way to suspend notifications on a given ContentProvider
's Uri
. The use case is:
Activity
is bound to a CursorAdapter
through a CursorLoader
.Service
may do a lot of batch, single-row updates on a ContentProvider
.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
.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With