安卓。内容提供商还是数据库?
我对这个问题有点困惑,使用 ContentProvider
还是 Database
更好。或者,如果我不想与其他应用程序共享任何数据,也没有什么区别。
如果我理解正确的话,内容提供程序基于 SQLite
DB,并且它们的内容也可能只能由我的应用程序访问。
你能给出一些解释吗?
非常感谢你,
穆尔
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然,存在一些有价值的问题需要提供商提供解决方案,特别是对于跨应用程序数据发布。例如,您需要使用内容提供程序向快速搜索框提供搜索建议。
但是,对于应用程序内部使用,我不喜欢。恕我直言,好处大于成本(例如,灵活性降低、额外开销)。
如果您确实实现了内容提供程序,请记住,默认情况下其他应用程序可以访问它们。。您需要在
元素中包含android:exported="false"
以使它们对您的应用程序私有。There certainly are worthwhile problems for which a provider is a solution, particularly for cross-app data publishing. For example, you need to use a content provider to supply search suggestions to a Quick Search Box.
However, for internal use within an application, I am not a fan. The benefits IMHO are outweighed by the costs (e.g., reduced flexibility, additional overhead).
If you do implement a content provider, bear in mind that they are accessible by other applications by default. You need to include
android:exported="false"
in the<provider>
element to make them private to your app.使用内容提供程序将为您提供更加模块化的设计,如果您将来某个时候想要访问其他应用程序的数据,那么您的生活会更轻松。
如果您确定仅一个应用程序需要这些数据,那么您最好直接对数据库进行操作。
您应该注意一个特定的 SQLite 限制,那就是 SQLite 仅限单用户。这真正意味着您需要保护数据库不被多个线程同时访问。这在内容提供商中通常不是问题,因为它们几乎总是具有单线程实现。
Using a content provider will give you a more modular design, and make your life easier if you at some point in future would like to reach the data from other applications.
If you are certain that the data will only ever be needed from one application, you might as well operate directly on the database.
There is one particular SQLite limitation you should be aware of and that is that SQLite is single-user only. What this really means is that you will need to guard your database from being accessed from multiple threads at the same time. This is generally not a problem in a content provider, since they almost always have a single-threaded implementation.
使用内容提供程序的原因如下此处。
总结:
Reasons to use content provider are here.
In summary: