Using ContentResolver

I am new to andriod domain and am in training. I have a few queries:

Do we have one ContentResolver object for each application? Is this a singleton object? Who controls this life cycle of an object? If it is singleton, how does it handle multiple requests for a ContentProvider request?

+7
source share
1 answer

From the album Alex Lockwood - http://www.androiddesignpatterns.com/2012/06/content-resolvers-and-content-providers.html

What is Content Resolver?

Content Resolver is the only global instance in your application that provides access to your (and other applications) content providers. Content Resolver behaves exactly as its name implies: it accepts requests from customers and solves these requests by sending them to the content provider with special authority. For this, a mapping from Content Providers is stored in the Content Resolver. This design is important because it provides a secure means of accessing the content providers of other applications.

The content editor includes the CRUD (create, read, update, delete) methods that correspond to the abstract methods (insert, delete, query, update) in the Content Provider class. The content repository does not know about the implementation of the content providers with which it interacts with (and does not need to know); each method is passed a URI that indicates that the content provider interacts with.

What is a content provider?

While the Content Resolver provides abstractions from content providers, content providers provide abstractions from a underlying data source (i.e., SQLite databases). They provide mechanisms for determining data security (i.e., by enforcing read and write permissions) and offer a standard interface that connects data in one process with code running in another process.

Content providers provide an interface for publishing and consuming data based on a simple URI model using content: // schemas. They allow you to double application tiers from the underlying data layers, making your application an agnostic data source by abstracting the primary data source.

Request Life

So what is a step-by-step process behind a simple request? In the form described above, when you request data from your database through a content provider, you do not directly communicate with the provider. Instead, you use the Content Resolver object to communicate with the provider. The specific sequence of events that occurs when a request is made below:

  • A call to getContentResolver().query(Uri, String, String, String, String) . A call triggers a Content Resolver request
    method, not ContentProvider's .

  • When the query method is called, the Content Resolver parses the uri argument and extracts its credentials.

  • Content Resolver sends a request to a content provider registered with a (unique) authority. This is done by calling the Content Provider query .

  • When the Content Provider query method is called, the query is executed and the cursor is returned (or an exception is thrown).
    as a result, the behavior is completely dependent on the content provider provider implementation.

+14
source

All Articles