Alex Rivera | Logout

using ContentProviderClient vs ContentResolver to access content provider

Asked 2011-02-22T22:39:57.993
67

The documentation on Android content providers describes using a ContentResolver, obtained from getContentResolver(), to access the content.

However there is also a ContentProviderClient, which can be obtained from getContentResolver().acquireContentProviderClient(authority). It seems to provide more or less the same methods available in the ContentResolver for accessing content from the provider.

When should I use a ContentProviderClient instead of just using the ContentResolver directly? What are the benefits?

Edit
Report

2 Answers

98

Your android device has many databases, each of which is identified by a unique Content Authority. This is the "domain name" equivalent part in the content:// uri -- everything before the first slash.

ContentResolver stores data providing a mapping from String contentAuthority to ContentProvider. When you call ContentResolver.query() or update() or what have you, the URI is parsed apart into its components, the contentAuthority string is identified, and contentResolver has to search that map for a matching string, and direct the query to the right provider. This expensive search occurs during every single call, because the URI might be different from call to call, with a different contentAuthority as well. Additionally, there may be some costs involved in setting up and tearing down a connection to that specific provider -- It can't be reused across calls. I'm not sure of the overhead involved there, that's some pretty deep OS level code.

By contrast, when you call acquireContentProviderClient(authority), that "what-provider do I need?" lookup is done once, and you are given a ContentProviderClient which is essentially a direct link to the ContentProvider. (There's a bit of glue between you and the provider that involves cross-thread communication and concurrency locking). However, when you use ContentProviderClient, you will talk directly to the Provider for the authority you requested. This removes the waste of constantly re-computing "which provider do I want?"

NOTE: Per acquireContentProviderClient() documentation: If you obtain a ContentProviderClient, "The caller must indicate that they are done with the provider by calling answered 2011-03-08T14:14:07.810

2

I found the following difference: I wrote my own custom contentprovider in app A. I wrote a homescreen Widget in App B. When I tried to access the ContentProvider of app A via a ContentResolver from my widget, I got an "failed to find provider info" error. When I instead would aquire a ContentProviderClient through the ContentResolver and query through the ContentProviderClient, it would work. I had to change nothing else, only use the ContentProviderClient instead of the ContentResolver. I have no real explanation for that behaviour and found no information on the internet, as to why it is like that. I do not know, if this is a special quirk of widgets, because I did not try it from an activity in app B (app B is a mere widget, with no activity).

answered 2012-03-09T21:17:29.633

Your Answer