简体   繁体   English

Android系统。 内容提供商或数据库?

[英]Android. Content provider or Database?

I'm a bit confused in the question, if it's better to use ContentProvider or Database . 如果使用ContentProviderDatabase更好,我在这个问题上有点困惑。 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. 如果我理解正确,内容提供商基于SQLite并且它们的内容也可能只能访问我的应用程序。

Can you give some explanations? 你能解释一下吗?

Thank you very much, 非常感谢你,

Mur 穆尔

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 (eg, 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. 您需要在<provider>元素中包含android:exported="false" ,以使其对您的应用程序保密。

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. 您应该注意一个特定的SQLite限制,那就是SQLite只是单用户。 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: 综上所述:

  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) 轻松更改基础数据源(您可以将数据库从Sqlite更改为Mongo或更改为JSON文件,而无需更改任何应用程序)
  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 利用一些Android类的功能(SyncAdapter,Loaders,CursorAdapter) - 这些类需要内容提供者,如果你没有,你就无法使用它们
  3. Allow many apps to access, use and modify a single data securely. 允许许多应用安全地访问,使用和修改单个数据。 (which is really the main reason for using it) (这是使用它的主要原因)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM