I understand that Content Providers are made to allow publicly sharing data between applications. However, I'm wondering if anyone has thoughts about making a Content Provider to use just within your own app. Would there be any advantages to doing this? Any disadvantages?
我了解内容提供商允许在应用程序之间公开共享数据。但是,我想知道是否有人想让内容提供商只在你自己的应用程序中使用。这样做有什么好处吗?有什么缺点?
In the past I've just implemented the SQliteOpenHelper to access data from my database, but I'm considering creating a Content Provider. I feel like the URI approach to requesting data is clear and concise. On the other hand, will using a Content Provider just for my application be redundant ( since within it I will have a SQliteOpenHelper class ) and more work than I need?
在过去,我刚刚实现了SQliteOpenHelper来访问我的数据库中的数据,但我正在考虑创建一个Content Provider。我觉得请求数据的URI方法简洁明了。另一方面,仅为我的应用程序使用内容提供程序是多余的(因为在其中我将有一个SQliteOpenHelper类)和比我需要的更多的工作?
8 个解决方案
#1
51
If you are not planning to share data, don't think about Content Providers. They are powerful but hard to write and it will be just silly to implement them if you are going to use them internally.
如果您不打算共享数据,请不要考虑内容提供商。它们很强大但很难编写,如果要在内部使用它们,实现它们将是愚蠢的。
However, I'm wondering if anyone has thoughts about making a Content Provider to use just within your own app.
但是,我想知道是否有人想让内容提供商只在你自己的应用程序中使用。
Of course... for instance, for an old TODO list app I wrote, I had to write a content provider to allow other apps retrieve and access the tasks states. It was part of the requirements, but more than that it made sense and made the app nicer.
当然......例如,对于我写的旧TODO列表应用程序,我必须编写内容提供程序以允许其他应用程序检索和访问任务状态。这是要求的一部分,但不仅仅是它有意义,并使应用程序更好。
#2
112
I would argue it is definitely a good idea to use a ContentProvider
even if you don't intend to make it public.
我认为使用ContentProvider绝对是一个好主意,即使你不打算公开它也是如此。
It's good practice to provide the extra level of abstraction over your data to make it easier to change internally. What if you decide to change the underlying database structure at a later time? If you use a ContentProvider
you can contain all the structural changes within it, where as if you don't use one, you are forced to change all areas of the code that are affected by the structural changes. Besides, it's nice to be able to re-use the same standard API for accessing data rather than littering your code with low-level access to the database.
最好为数据提供额外的抽象级别,以便更容易在内部进行更改。如果您决定稍后更改基础数据库结构,该怎么办?如果使用ContentProvider,则可以包含其中的所有结构更改,就像您不使用它一样,您将*更改受结构更改影响的代码的所有区域。此外,能够重复使用相同的标准API来访问数据,而不是通过对数据库的低级访问来乱丢代码,这很好。
Also, there is always the chance that you might want to expose your data in the future. If you don't use a ContentProvider
up front, it will be much harder to retrofit it in at a later date.
此外,您可能希望将来公开您的数据。如果您没有预先使用ContentProvider,那么在以后更改它将会更加困难。
Then, there's the other parts of the Android where ContentProvider
's are required/recommended such as when using SyncAdapter
s and if you want an App Widget that involves data access for instance.
然后,Android的其他部分需要/推荐ContentProvider,例如使用SyncAdapters时,如果你想要一个涉及数据访问的App Widget。
In summary, there is very little overhead involved in writing a ContentProvider
up front (once you have learned the API which is a good idea anyway) so it makes sense to do so, even for private data.
总之,预先编写ContentProvider所涉及的开销非常小(一旦你学会了API这是一个好主意),所以这样做是有意义的,即使是私有数据也是如此。
#3
7
Take a look at the MOTODEV Studio for Eclipse. It is a development environment that extends Eclipse. They have a tool where you can automatically generate a content provider for a database. If a content provider makes it easier to access your data and it doesn't have a significant impact on performance go ahead and use it. In most scenarios this will be the case.
看看MOTODEV Studio for Eclipse。它是一个扩展Eclipse的开发环境。他们有一个工具,您可以在其中自动为数据库生成内容提供程序。如果内容提供商可以更轻松地访问您的数据,并且不会对性能产生重大影响,请继续使用它。在大多数情况下,情况就是这样。
#4
4
I agree ContentProviders are a little difficult to grasp but they are definitely helpful, even if you want to use them internally for you own app. The best thing about it is that you can customize the contentproviders for suitable URIs.
我同意ContentProviders有点难以掌握,但它们肯定是有用的,即使你想在内部使用它们为你自己的应用程序。关于它的最好的事情是你可以自定义内容提供者以获得合适的URI。
Here's a scenario where you may have 5 tables in your database, but you need to join a few of them in certain orders before using them. And make a content URI for each of these joins. You could then each use these URIs as a table :)
在这种情况下,您的数据库中可能有5个表,但在使用它们之前,您需要以某些顺序加入其中的一些表。并为每个连接创建一个内容URI。然后你可以将这些URI用作表格:)
I suggest you go ahead with Content Provider, you'll be amazed to see how powerful it is.
我建议你继续使用内容提供商,你会惊讶地发现它有多强大。
#5
4
In short,Content Providers
helps in managing your data effectively. I would suggest to use them for the following reasons.
简而言之,内容提供商可以帮助您有效地管理数据。我建议使用它们,原因如下。
- It acts as an abstraction layer between your UI and database. You can implement data validation in ContentProviders to validate the data entered by the user. It also lets you to modify the structure of the database without touching the UI and other parts.
- 它充当UI和数据库之间的抽象层。您可以在ContentProviders中实现数据验证,以验证用户输入的数据。它还允许您修改数据库的结构,而无需触及UI和其他部分。
- They play along nicely with other android framework classes like
SyncAdapter
. For eg., you can automatically refresh a list, when a value in a database changes using ContentProviders along withCursorLoader
. Without ContentProviders you have to implement a lot of functionalities like these on your own. - 它们与其他Android框架类(如SyncAdapter)很好地配合使用。例如,当数据库中的值使用ContentProviders和CursorLoader更改时,您可以自动刷新列表。如果没有ContentProviders,您必须自己实现许多这样的功能。
- We can safely expose our private data to other apps. Using ContentProviders will allow us to share our data easily and safely with other apps.
- 我们可以安全地将私人数据公开给其他应用。使用ContentProviders将允许我们与其他应用轻松安全地共享我们的数据。
So even if you don't need any of these functionalities now, you might need them in future and its good to go the extra mile and implement them right now.
因此,即使您现在不需要任何这些功能,您将来也可能需要它们,并且现在可以更好地实施它们。
#6
2
In my view point, the content-provider comes with plenty of advantages leave alone just sharing data with other apps. If you need to synchronize with the server using a Sync-Adapter, use google cloud messaging, auto update the UI when the underlying data in the DB changes using Loaders, implement search, use widgets... then the content provider is for you.
在我看来,内容提供商具有很多优势,只需与其他应用程序共享数据。如果您需要使用同步适配器与服务器同步,请使用谷歌云消息传递,当数据库中的基础数据使用加载器更改时自动更新UI,实现搜索,使用小部件...然后内容提供商适合您。
I prefer you follow the guideline on because one day you may need to implement some of the above features attached to the content-provider
我更喜欢您遵循指南,因为有一天您可能需要实现附加到内容提供商的一些上述功能
By the way, you can quickly build you database and CP in less than 5 minutes using content provider generator
顺便说一句,您可以使用内容提供程序生成器在不到5分钟的时间内快速构建数据库和CP
#7
1
As said in documentation: Creating a Content provider
如文档中所述:创建内容提供商
You don't need a provider to use an SQLite database if the use is entirely within your own application.
如果使用完全在您自己的应用程序中,则不需要提供程序来使用SQLite数据库。
So why bother developing this overhead? You want easier and faster development, right? So one layer of abstraction (SQLiteOpenHelper descendent) is enough.
那么为什么要开发这个开销呢?你想要更容易和更快的发展,对吧?所以一层抽象(SQLiteOpenHelper后代)就足够了。
See Occam's Razor Do not make an entities without very good reason.
看奥卡姆的剃刀不要没有很好的理由做一个实体。
#8
-1
Do not use content provider if do not wish to share data with other apps. Use simple sqlitedatabase to perform database operations. Be careful while using content providers for storing confidential data because your confidential information may be accessed by other apps
如果不希望与其他应用共享数据,请勿使用内容提供商。使用简单的sqlitedatabase执行数据库操作。使用内容提供商存储机密数据时要小心,因为其他应用可能会访问您的机密信息
#1
51
If you are not planning to share data, don't think about Content Providers. They are powerful but hard to write and it will be just silly to implement them if you are going to use them internally.
如果您不打算共享数据,请不要考虑内容提供商。它们很强大但很难编写,如果要在内部使用它们,实现它们将是愚蠢的。
However, I'm wondering if anyone has thoughts about making a Content Provider to use just within your own app.
但是,我想知道是否有人想让内容提供商只在你自己的应用程序中使用。
Of course... for instance, for an old TODO list app I wrote, I had to write a content provider to allow other apps retrieve and access the tasks states. It was part of the requirements, but more than that it made sense and made the app nicer.
当然......例如,对于我写的旧TODO列表应用程序,我必须编写内容提供程序以允许其他应用程序检索和访问任务状态。这是要求的一部分,但不仅仅是它有意义,并使应用程序更好。
#2
112
I would argue it is definitely a good idea to use a ContentProvider
even if you don't intend to make it public.
我认为使用ContentProvider绝对是一个好主意,即使你不打算公开它也是如此。
It's good practice to provide the extra level of abstraction over your data to make it easier to change internally. What if you decide to change the underlying database structure at a later time? If you use a ContentProvider
you can contain all the structural changes within it, where as if you don't use one, you are forced to change all areas of the code that are affected by the structural changes. Besides, it's nice to be able to re-use the same standard API for accessing data rather than littering your code with low-level access to the database.
最好为数据提供额外的抽象级别,以便更容易在内部进行更改。如果您决定稍后更改基础数据库结构,该怎么办?如果使用ContentProvider,则可以包含其中的所有结构更改,就像您不使用它一样,您将*更改受结构更改影响的代码的所有区域。此外,能够重复使用相同的标准API来访问数据,而不是通过对数据库的低级访问来乱丢代码,这很好。
Also, there is always the chance that you might want to expose your data in the future. If you don't use a ContentProvider
up front, it will be much harder to retrofit it in at a later date.
此外,您可能希望将来公开您的数据。如果您没有预先使用ContentProvider,那么在以后更改它将会更加困难。
Then, there's the other parts of the Android where ContentProvider
's are required/recommended such as when using SyncAdapter
s and if you want an App Widget that involves data access for instance.
然后,Android的其他部分需要/推荐ContentProvider,例如使用SyncAdapters时,如果你想要一个涉及数据访问的App Widget。
In summary, there is very little overhead involved in writing a ContentProvider
up front (once you have learned the API which is a good idea anyway) so it makes sense to do so, even for private data.
总之,预先编写ContentProvider所涉及的开销非常小(一旦你学会了API这是一个好主意),所以这样做是有意义的,即使是私有数据也是如此。
#3
7
Take a look at the MOTODEV Studio for Eclipse. It is a development environment that extends Eclipse. They have a tool where you can automatically generate a content provider for a database. If a content provider makes it easier to access your data and it doesn't have a significant impact on performance go ahead and use it. In most scenarios this will be the case.
看看MOTODEV Studio for Eclipse。它是一个扩展Eclipse的开发环境。他们有一个工具,您可以在其中自动为数据库生成内容提供程序。如果内容提供商可以更轻松地访问您的数据,并且不会对性能产生重大影响,请继续使用它。在大多数情况下,情况就是这样。
#4
4
I agree ContentProviders are a little difficult to grasp but they are definitely helpful, even if you want to use them internally for you own app. The best thing about it is that you can customize the contentproviders for suitable URIs.
我同意ContentProviders有点难以掌握,但它们肯定是有用的,即使你想在内部使用它们为你自己的应用程序。关于它的最好的事情是你可以自定义内容提供者以获得合适的URI。
Here's a scenario where you may have 5 tables in your database, but you need to join a few of them in certain orders before using them. And make a content URI for each of these joins. You could then each use these URIs as a table :)
在这种情况下,您的数据库中可能有5个表,但在使用它们之前,您需要以某些顺序加入其中的一些表。并为每个连接创建一个内容URI。然后你可以将这些URI用作表格:)
I suggest you go ahead with Content Provider, you'll be amazed to see how powerful it is.
我建议你继续使用内容提供商,你会惊讶地发现它有多强大。
#5
4
In short,Content Providers
helps in managing your data effectively. I would suggest to use them for the following reasons.
简而言之,内容提供商可以帮助您有效地管理数据。我建议使用它们,原因如下。
- It acts as an abstraction layer between your UI and database. You can implement data validation in ContentProviders to validate the data entered by the user. It also lets you to modify the structure of the database without touching the UI and other parts.
- 它充当UI和数据库之间的抽象层。您可以在ContentProviders中实现数据验证,以验证用户输入的数据。它还允许您修改数据库的结构,而无需触及UI和其他部分。
- They play along nicely with other android framework classes like
SyncAdapter
. For eg., you can automatically refresh a list, when a value in a database changes using ContentProviders along withCursorLoader
. Without ContentProviders you have to implement a lot of functionalities like these on your own. - 它们与其他Android框架类(如SyncAdapter)很好地配合使用。例如,当数据库中的值使用ContentProviders和CursorLoader更改时,您可以自动刷新列表。如果没有ContentProviders,您必须自己实现许多这样的功能。
- We can safely expose our private data to other apps. Using ContentProviders will allow us to share our data easily and safely with other apps.
- 我们可以安全地将私人数据公开给其他应用。使用ContentProviders将允许我们与其他应用轻松安全地共享我们的数据。
So even if you don't need any of these functionalities now, you might need them in future and its good to go the extra mile and implement them right now.
因此,即使您现在不需要任何这些功能,您将来也可能需要它们,并且现在可以更好地实施它们。
#6
2
In my view point, the content-provider comes with plenty of advantages leave alone just sharing data with other apps. If you need to synchronize with the server using a Sync-Adapter, use google cloud messaging, auto update the UI when the underlying data in the DB changes using Loaders, implement search, use widgets... then the content provider is for you.
在我看来,内容提供商具有很多优势,只需与其他应用程序共享数据。如果您需要使用同步适配器与服务器同步,请使用谷歌云消息传递,当数据库中的基础数据使用加载器更改时自动更新UI,实现搜索,使用小部件...然后内容提供商适合您。
I prefer you follow the guideline on because one day you may need to implement some of the above features attached to the content-provider
我更喜欢您遵循指南,因为有一天您可能需要实现附加到内容提供商的一些上述功能
By the way, you can quickly build you database and CP in less than 5 minutes using content provider generator
顺便说一句,您可以使用内容提供程序生成器在不到5分钟的时间内快速构建数据库和CP
#7
1
As said in documentation: Creating a Content provider
如文档中所述:创建内容提供商
You don't need a provider to use an SQLite database if the use is entirely within your own application.
如果使用完全在您自己的应用程序中,则不需要提供程序来使用SQLite数据库。
So why bother developing this overhead? You want easier and faster development, right? So one layer of abstraction (SQLiteOpenHelper descendent) is enough.
那么为什么要开发这个开销呢?你想要更容易和更快的发展,对吧?所以一层抽象(SQLiteOpenHelper后代)就足够了。
See Occam's Razor Do not make an entities without very good reason.
看奥卡姆的剃刀不要没有很好的理由做一个实体。
#8
-1
Do not use content provider if do not wish to share data with other apps. Use simple sqlitedatabase to perform database operations. Be careful while using content providers for storing confidential data because your confidential information may be accessed by other apps
如果不希望与其他应用共享数据,请勿使用内容提供商。使用简单的sqlitedatabase执行数据库操作。使用内容提供商存储机密数据时要小心,因为其他应用可能会访问您的机密信息