针对Android不同的线程数据库访问 [英] Database access from different threads in Android

查看:378
本文介绍了针对Android不同的线程数据库访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务从AsyncTasks互联网的下载数据。它分析数据,并将其存储在数据库中。该服务持续运行。

I have a Service that downloads data from the Internet in AsyncTasks. It parses the data and store it in a db. The Service runs continuously.

有是一个活动试图在服务处于写它从数据库中读取的变化。

There is a change that a Activity attempts to read from the db while the Service is writing to it.

我有几种方法,用于写入和读取的数据库帮手。莫非这个事业的问题?潜在试图从两个不同的线程打开分贝

I have a database helper with several methods for writing and reading. Could this cause problems? Potentially trying to open the db from two different threads?

推荐答案

有已经讲过,写关于分贝并发问题很多书。

there has been talked and written many books about concurrency problems in db .

但是,使用计算器像维基百科我已经发现了一些有趣的thing:

But using stackoverflow like a wikipedia i have found some interesting thing:

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

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.

则数据库会很好的保护他人线程坏消息是,你必须管理这些例外,然后可能是一个肮脏的工作,如果你有一个大的数据库

Then the database is well protected from others threads . The bad news is that you must manage those exceptions , and can be a dirty work if you have a big database (meaning with big many tables with much data exchange)

这篇关于针对Android不同的线程数据库访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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