简体   繁体   English

Android - sqlite内容提供程序和多线程

[英]Android - sqlite content providers and multithreading

I'm a little confused about content providers. 我对内容提供商有点困惑。 If I have multiple activities in my application do they each get their own instance of the content provider? 如果我在我的应用程序中有多个活动,他们每个人都会获得自己的内容提供者实例吗? it's just essentially a class/interface? 它本质上只是一个类/接口?

In one activity I will have many threads simultaneously writing to the database. 在一个活动中,我将有许多线程同时写入数据库。 How do I deal with allowing one thread to write at a time? 我如何处理允许一个线程一次写入?

Do I just catch SQLiteDatabaseLockedException, put the thread to sleep then retry? 我只是捕获SQLiteDatabaseLockedException,将线程置于休眠状态然后重试? Or is there a better way? 或者,还有更好的方法?

Are the database locks released when an activity pauses/is destroyed? 当活动暂停/被销毁时是否释放数据库锁? If so could I just create a synchronized lock against the content provider itself? 如果是这样,我可以创建一个针对内容提供商本身的同步锁?

If you work directly with databases and have multiple writers from different threads you may run into concurrency issues. 如果您直接使用数据库并拥有来自不同线程的多个编写器,则可能会遇到并发问题。

The ContentProvider can be accessed from several programs at the same time, therefore you must implement the access thread-safe. ContentProvider可以同时从多个程序访问,因此您必须实现访问线程安全。 The easiest way is to use the keyword synchronized in front of all methods of the ContentProvider , so that only one thread can access these methods at the same time. 最简单的方法是在ContentProvider的所有方法前面使用synchronized ,这样只有一个线程可以同时访问这些方法。

If you do not require that Android synchronizes data access to the ContentProvider , set the android:multiprocess=true attribute in your <provider> definition in the AndroidManifest.xml file. 如果您不要求Android同步对ContentProvider数据访问,请在AndroidManifest.xml文件的<provider>定义中设置android:multiprocess=true属性。 This permits an instance of the provider to be created in each client process, eliminating the need to perform interprocess communication (IPC). 这允许在每个客户端进程中创建提供者的实例,从而消除了执行进程间通信(IPC)的需要。

Oops, I lost my unregistered user cookie so can't vote Femi's answer correct. 哎呀,我丢失了未注册的用户cookie,因此无法将Femi的答案投票正确。

The documentation http://developer.android.com/guide/topics/providers/content-providers.html confirms this with "When a query is initiated, the Android system identifies the content provider that's the target of the query and makes sure that it is up and running. The system instantiates all ContentProvider objects; you never need to do it on your own. In fact, you never deal directly with ContentProvider objects at all. Typically, there's just a single instance of each type of ContentProvider. But it can communicate with multiple ContentResolver objects in different applications and processes. The interaction between processes is handled by the ContentResolver and ContentProvider classes. " 文档http://developer.android.com/guide/topics/providers/content-providers.html通过以下方式确认:“启动查询时,Android系统会识别作为查询目标的内容提供商,并确保它已启动并运行。系统实例化所有ContentProvider对象;您永远不需要自己完成。实际上,您根本不会直接处理ContentProvider对象。通常,每种类型的ContentProvider只有一个实例。但是它可以与不同应用程序和进程中的多个ContentResolver对象进行通信。进程之间的交互由ContentResolver和ContentProvider类处理。“

If you are using a ContentProvider, I believe you don't care how many threads are reading/writing: the Android platform handles marshalling all your calls onto a single thread and sorting out synchronization and locking. 如果您使用的是ContentProvider,我相信您并不关心有多少线程正在读/写:Android平台会处理将所有调用编组到一个线程上并整理出同步和锁定。 You just open your database and read/write into it, and everyone else talks through the ContentProvider interface. 您只需打开数据库并读/写,其他人都可以通过ContentProvider界面进行通信。

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

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