安卓。内容提供商还是数据库?

发布于 2024-10-04 05:31:37 字数 226 浏览 2 评论 0 原文

我对这个问题有点困惑,使用 ContentProvider 还是 Database 更好。或者,如果我不想与其他应用程序共享任何数据,也没有什么区别。

如果我理解正确的话,内容提供程序基于 SQLite DB,并且它们的内容也可能只能由我的应用程序访问。

你能给出一些解释吗?

非常感谢你,

穆尔

I'm a bit confused in the question, if it's better to use ContentProvider or Database. Or it makes no difference if I don't want to share any data with other applications.

If I've understood it right, content providers based on SQLite DBs and it's also possible that content of them is only accessable for my application.

Can you give some explanations?

Thank you very much,

Mur

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停滞 2024-10-11 05:31:37

当然,存在一些有价值的问题需要提供商提供解决方案,特别是对于跨应用程序数据发布。例如,您需要使用内容提供程序向快速搜索框提供搜索建议。

但是,对于应用程序内部使用,我不喜欢。恕我直言,好处大于成本(例如,灵活性降低、额外开销)。

如果您确实实现了内容提供程序,请记住,默认情况下其他应用程序可以访问它们。。您需要在 元素中包含 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.

行至春深 2024-10-11 05:31:37

使用内容提供程序将为您提供更加模块化的设计,如果您将来某个时候想要访问其他应用程序的数据,那么您的生活会更轻松。
如果您确定仅一个应用程序需要这些数据,那么您最好直接对数据库进行操作。

您应该注意一个特定的 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.

注定孤独终老 2024-10-11 05:31:37

使用内容提供程序的原因如下此处

总结:

  1. 轻松更改底层数据源(您可以将数据库从 Sqlite 更改为 Mongo 或 JSON 文件,而无需更改任何应用程序)
  2. 利用某些 Android 类(SyncAdapter、Loaders、CursorAdapter)的功能 - 这些类需要内容提供程序和您如果您没有允许
  3. 多个应用程序安全地访问、使用和修改单个数据,则无法使用它们。 (这确实是使用它的主要原因)

Reasons to use content provider are here.

In summary:

  1. Easily change the underlying data source (you can change your db from Sqlite to Mongo or to a JSON file without any app changes)
  2. Leverage functionality of some Android Classes (SyncAdapter, Loaders, CursorAdapter) - These classes require content provider and you cant use them if you dont have one
  3. Allow many apps to access, use and modify a single data securely. (which is really the main reason for using it)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文