A Content Provider is a component that interacts with a SQLIteOpenHelper or repository. It acts as a mediator between ContentResolver and SQLiteOpenHelper The app don’t know that where is data stored and in which format it is stored and from where the data is coming accessed into the app.
A content provider: Offers the following features to the app
- It Separates data from the app means no need to develop database within the app
- Provides a standard way for accessing the data from the database, server or cloud
- It allows other apps to access data securely.
- The data may be stored in database, server, cloud or in any repository or in any file system.
What is a Content Resolver?
To get data from the database the app is not communicating directly with the database or ContentProvider. To communicate with the ContentProvider we are using ContentResolver Into the App. By using Content Resolver we are interacting with a content provider through a request query to communicate the database, Then ContentProvider gets data from the database through SQLiteOpenHelper passes the same to the ContentResolver based on the ContentResolvers request.
The ContentResolver object provides query(), insert(), update(), and delete() methods for accessing data from the database through a content provider and also insert data into the database through ContentProvider.
Each request consists of a URI and a SQL-like query, and the response is a Cursor object means we are getting data in the form of cursor object when we query a “select” method to the database via ContentProvider.
Why Content Providers are good for?
Content providers are useful for apps that want to make data available to other apps.
- With the help of content provider, you can allow multiple other apps to access data securely, and uses, and modifies a single data source that your app provides. Examples: Warehouse inventory for retail stores, game scores, or a collection of physics problems for colleges.
- Using Content Provider we can control or limit other apps to access data with the provided permissions means some apps means we can allow some apps modify the data and some other apps can see data only means they can not modify the data such things we can control using content provider.
- Using content Provider the data can be stored independently from the app. means the Content Provider lies between User Interface and the stored data. we can also change the data stored place without disturbing the app or application code. means now we are storing the in local database, If we want to store same data into any server side database without touching the Appliction code this is the advantage of Content provider.
- The benefit of separating data from the user interface with the help of content provider is that development teams can work independently on the user interface means one development team works on user interface and other team works on data repository of your app means same app to speed of the development.
- While developing complex application there are teams are divided into modules and works independently to speed of development
- There are other classes that interact with a content provider other than content resolver i.e. CursorLoader. It is used to load data in the background from the database.
Note: Content provider is used to provide or access data securely to the other apps. If your app is the only one using the data then no need a content provider.
Data and Open Helper:
- The data is stored in repository or in a database, a file, on the internet, generated dynamically, or even a mix of these. OpenHelper class directly interacts with the database or repository. The content provider directly interacts with the OpenHelper passes the request received from the Content Resolver to the OpenHelper. OpenHelper gets data from the database based on the Content Provider request sends it to the content provider then to the Content Resolver to the app or User Interface. With the help of Content Provider we are saving data into the database and Content Provider API’s.
- The contract is a public class that contains important information about the content provider to other apps. This mainly has URI schemes, important constants, and the structure of the data that will be returned based on the request.. For example, retail apps has the names of the columns that contain the price and name of a product, and the URI for retrieving an inventory item by part number.