Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContentResolver usage

I am new to andriod domain and is in learning phase. I got couple of queries:

Do we have single ContentResolver object per app? Is it a singleton object? Who manages this object lifecycle? If it's singleton, how does it handles multiple request of querying a ContentProvider?

like image 525
loc Avatar asked Jan 16 '13 10:01

loc


People also ask

What is the ContentResolver class used for?

The ContentResolver methods provide the basic "CRUD" (create, retrieve, update, and delete) functions of persistent storage. A common pattern for accessing a ContentProvider from your UI uses a CursorLoader to run an asynchronous query in the background.

What does ContentResolver do in Android?

The Content Resolver behaves exactly as its name implies: it accepts requests from clients, and resolves these requests by directing them to the content provider with a distinct authority. To do this, the Content Resolver stores a mapping from authorities to Content Providers.

What does ContentResolver query () return?

ContentResolver. query() will return null in the following cases: If you try to pass column names which don't exist in the database (a very common case is when developers use constants as column names, because they look similar to columns). It is likely to be null because your URI argument is invalid.

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

ContentProvider is mainly used for access data from one application to another application. For example by using ContentProvider we can get phone contacts,call log from phone to our own application in android. we can also access data which are stored in (sqlite)databases.


1 Answers

From Alex Lockwood's Blog - http://www.androiddesignpatterns.com/2012/06/content-resolvers-and-content-providers.html

What is the Content Resolver?

The Content Resolver is the single, global instance in your application that provides access to your (and other applications') content providers. The Content Resolver behaves exactly as its name implies: it accepts requests from clients, and resolves these requests by directing them to the content provider with a distinct authority. To do this, the Content Resolver stores a mapping from authorities to Content Providers. This design is important, as it allows a simple and secure means of accessing other applications' Content Providers.

The Content Resolver includes the CRUD (create, read, update, delete) methods corresponding to the abstract methods (insert, delete, query, update) in the Content Provider class. The Content Resolver does not know the implementation of the Content Providers it is interacting with (nor does it need to know); each method is passed an URI that specifies the Content Provider to interact with.

What is a Content Provider?

Whereas the Content Resolver provides an abstraction from the application's Content Providers, Content Providers provides an abstraction from the underlying data source (i.e. a SQLite database). They provide mechanisms for defining data security (i.e. by enforcing read/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 around a simple URI addressing model using the content:// schema. They enable you to decouble your application layers from the underlying data layers, making your application data-source agnostic by abstracting the underlying data source.

The Life of a Query

So what exactly is the step-by-step process behind a simple query? As described above, when you query data from your database via the content provider, you don't communicate with the provider directly. Instead, you use the Content Resolver object to communicate with the provider. The specific sequence of events that occurs when a query is made is given below:

  • A call to getContentResolver().query(Uri, String, String, String, String) is made. The call invokes the Content Resolver's query
    method, not the ContentProvider's.

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

  • The Content Resolver directs the request to the content provider registered with the (unique) authority. This is done by calling the
    Content Provider's query method.

  • When the Content Provider's query method is invoked, the query is performed and a Cursor is returned (or an exception is thrown). The
    resulting behavior depends entirely on the Content Provider's
    implementation.

like image 75
Dharmendra Avatar answered Sep 21 '22 17:09

Dharmendra