Android的 - sqlite的内容提供商和多线程 [英] Android - sqlite content providers and multithreading

查看:122
本文介绍了Android的 - sqlite的内容提供商和多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对内容提供商有点困惑。 如果我在我的应用程序的多个活动?他们各自拥有的内容提供商的实例?它只是本质上是一个类/接口?

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?

我正好赶上SQLiteDatabaseLockedException,把线程休眠然后重试? 还是有更好的办法吗?

Do I just catch SQLiteDatabaseLockedException, put the thread to sleep then retry? 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.

的ContentProvider 可以从几个程序访问在同一时间,因此,你必须实现访问线程安全的。最简单的方法是使用关键字同步中的所有方法前面的的ContentProvider ,以便只有一个线程可以访问同时这些方法。

The ContentProvider can be accessed from several programs at the same time, therefore you must implement the access thread-safe. 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.

如果你不要求Android的同步数据访问的ContentProvider ,将机器人:多进程= TRUE 在AndroidManifest.xml文件定义;属性在<供应商&GT。这允许将在每一个客户机进程中创建的提供者的一个实例,无需执行进程间通信(IPC)。

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. This permits an instance of the provider to be created in each client process, eliminating the need to perform interprocess communication (IPC).

这篇关于Android的 - sqlite的内容提供商和多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆