When to use a content provider

I understand that content providers are designed to share data between applications. However, I am wondering if anyone has any thoughts on how to use the content provider only in their application. Were there any advantages to this? Any flaws?

In the past, I just implemented SQliteOpenHelper to access data from my database, but I am considering creating a Content Provider. I feel that the URI approach to data requests is clear and concise. On the other hand, using the Content Provider only for my application will be redundant (since I will have the SQliteOpenHelper class in it) and more work than I need?

+85
android android-contentprovider
Feb 08 '11 at 18:15
source share
8 answers

If you are not going to share data, do not think about content providers. They are powerful, but difficult to write, and it would be foolish to implement them if you intend to use them internally.

However, I am wondering if anyone has any thoughts on how to make the content provider used only in your own application.

Of course ... for example, for the old TODO application that I wrote, I had to write a content provider so that other applications could receive and access task states. This was part of the requirements, but more than that, it made sense and made the application more enjoyable.

+46
Feb 08 '11 at 18:20
source share

I would say that it is definitely nice to use ContentProvider , even if you are not going to publish it.

It’s good practice to provide an extra layer of abstraction over your data to facilitate internal change. What if you decide to change the basic structure of the database later? If you use a ContentProvider , you can contain all the structural changes inside it, where, as if you hadn't used it, you are forced to change all areas of the code that are affected by the structural changes. It’s also nice to be able to reuse the same standard API to access data, rather than clog your code with low-level database access.

In addition, there is always the possibility that you will want to publish your data in the future. If you are not using the ContentProvider front panel, it will be much harder to update it later.

Then there are required and recommended other parts of Android, where the ContentProvider , for example, when using the SyncAdapter , and if you want an application widget that includes data access, for example.

In general, there is very little overhead when writing ContentProvider up (after you learned the API, which is a good idea), so it makes sense to do this even for private data.

+106
Mar 19 2018-12-12T00:
source share

Take a look at the MOTODEV studio for Eclipse. This is a development environment that extends Eclipse. They have a tool in which you can automatically generate a content provider for the database. If the content provider simplifies access to your data and does not have a significant impact on performance, use it. In most cases this will be the case.

+6
Apr 17 '11 at 6:28
source share

I agree that ContentProviders are a little hard to understand, but they are definitely useful, even if you want to use them internally for your application. The best part is that you can configure content providers for the right URI.

Here's a scenario where you can have 5 tables in your database, but before using them you need to join several of them. And create a URI for each of these joins. Then you can use these URIs as a table :)

I suggest you continue working with the Content Provider, you will be amazed at how effective it is.

+4
Mar 20 2018-12-12T00:
source share

In my opinion, the content provider comes with many advantages, leaving alone just the exchange of data with other applications. If you need to synchronize with the server using the synchronization adapter, use the Google cloud messaging service, automatically update the user interface when the basic data in the database is changed using Loaders, implement searches, use widgets ... then the content provider is for you.

I prefer you to follow the guide, because one day you may need to implement some of the above functions that come with the content provider

By the way, you can quickly build your database and CP in less than 5 minutes using the content provider generator

+2
Nov 07 '15 at 15:01
source share

In short, Content Providers help you manage your data efficiently . I would suggest using them for the following reasons.

  • It acts as an abstraction layer between your user interface and the database . You can implement data validation in ContentProviders to validate user input. It also allows you to change the structure of the database without touching the user interface and other parts.
  • They blend well with other Android framework classes such as SyncAdapter . For example, you can automatically update the list when the value in the database is changed using ContentProviders along with CursorLoader . Without ContentProviders, you have to implement many features like these yourself.
  • We can safely expose our personal data in other applications . Using ContentProviders will allow us to easily and safely share our data with other applications.

So, even if you don’t need any of these features now, you may need them in the future, and it’s good to go the extra mile and realize them right now.

+2
Nov 12 '16 at 8:26
source share

As stated in the documentation: Creating a content provider

You do not need a provider to use the SQLite database if it is used entirely within your own application.

So why bother with this overhead? You need to grow faster and easier, right? Thus, one level of abstraction is sufficient (SQLiteOpenHelper descendent).

See Occasional Razor. Don't make entities without good reason.

-one
Sep 01 '16 at 12:02
source share

Do not use a content provider if you do not want to share data with other applications. To perform database operations, use a simple sqlited database. Be careful when using content providers to store sensitive data, as other applications may be available to your sensitive information.

-2
Jul 22 '15 at 7:37
source share



All Articles