无例外的Andr​​oid源码并发 [英] Android sqlite concurrency without exceptions

查看:121
本文介绍了无例外的Andr​​oid源码并发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sqlite的Andr​​oid上,您可以从多个特效访问数据库的读取,但如果你正在从一个进程写入,读取和其他特效写,因为第一次写对数据库的锁会抛出异常。

Sqlite on android lets you access the database from multiple procs for reads, but if you're currently writing from one process, reads and writes from other procs will throw an exception because the first write has a lock on the db.

所谓特效我的意思是,在相同的应用程序的其他线程。

By "procs" I mean other threads in the same app.

有没有一种标准的方式来有其他线程只是等待,直到数据库再次可用,可能带有指定超时,而不是抛出一个异常?

Is there a standard way to have the other threads simply wait until the database is available again, possibly with a specified timeout, rather than throwing an exception?

预见到:你为什么这样做呀?答案,它现在的样子,我们正在做的,仅此而已。我们也不会使用内容提供商。只是希望有一个方法来同步数据库访问。

Anticipating the "why are you doing it that way?" answers, its just the way we're doing it, and that's it. We're also not going to use a content provider. Just want a way to synchronize db access.

假设有没有标准的方式来做到这一点,我们可能会风写周围的分贝的包装要求做一些线程同步。

Assuming there's no standard way to do it, we'll probably wind up writing a wrapper around the db calls to do some thread synchronization.

推荐答案

只要你使用相同的 SQLiteDatabase 对象,同步完成了你。

So long as you're using the same SQLiteDatabase object, the synchronisation is done for you.

所以,如果你通过一个单独访问对象,应该没有问题。虽然你可能想,如果你想实现一个超时添加一些进一步的逻辑,如等待/通知或类似的东西。

So if you access the object via a singleton, there should be no problem. Though you may want to add some further logic if you want to implement a timeout, e.g. wait/notify or something similar.

这篇关于无例外的Andr​​oid源码并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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