那么多线程在Android的SQLite的? [英] What about multithreading in Android SQLite?

查看:135
本文介绍了那么多线程在Android的SQLite的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我不得不实施一些UI和同步服务。 它在后台运行,并更新数据。同步服务是不是很简单,它采用多线程处理。

In my app I've got to implement some UI and Sync service. It runs in the background and updates data. Sync service is not very simple, it uses multithreading.

所以,这里是我的故事: 当我开始开发这个程序,我不知道源码什么,所以我就没有用在Java中的任何线程同步。结果:我有许多例外,如SQLiteException:数据库被锁定:BEGIN EXCLUSIVE;

So, here is my story: When I started developing this app I didn't know anything about sqlite, so I just didn't use any thread synchronization in Java. Result: I got many Exceptions like "SQLiteException: database is locked: BEGIN EXCLUSIVE;"

然后我同步我的所有交易与常规的Java同步(){}块。一切都好多了。但我用游标实施的CursorAdapter我的名单。所以,有时候我得到了同样的SQLiteException:数据库被锁定:BEGIN EXCLUSIVE;

Then I synchronized all my transactions with regular Java synchronized () {} block. Everything got much better. But I used Cursors for implementing CursorAdapter for my lists. So, sometimes I was getting the same "SQLiteException: database is locked: BEGIN EXCLUSIVE;"

最后我创造它能够处理所有线程安全的东西的小线程安全的sqlite的工具。另外我必须使用像ArrayAdapter(读取光标所有数据,看完后关闭它也同步此块)为我的UI。因此,它正在确定

I ended creating a small thread safe sqlite utility which handles all that thread-safe stuff. Also I've got to use something like ArrayAdapter (read all data from Cursor and close it after reading, also synchronize this block) for my UI. So, it is working OK

不过,我不喜欢这样的方式来处理UI,UI事业得到了真正的慢与此解决方案 - 读取光标一些数据量是相当快的,但比使用的CursorAdapter

But, I don't like such way to handle UI, cause UI got really slower with this solution - reading some amount of data from cursor is rather fast, but is slower than using CursorAdapter

那么,谁得到了这个问题的解决方案? 谢谢

So, who got the solution for this question? Thank you

推荐答案

所以,最后出来的解决方案。在这里,它是。

So, finally came out to the solution. Here it is.

我看了一些论坛上,谷歌集团和发现,SQLite数据库应该被打开一次。所以,我实现了这个使用单。

I read some forums, google groups and found out that sqlite database should be opened only once. So, I implemented this using singleton.

另外,我实现了一些分贝code同步所有写操作(以prevent多线程执行写入操作一次)。 我不在乎打开游标,从他们阅读。

Also, I implemented some db code to synchronize all write operations (to prevent many threads execute write operations at one time). And I don't care about opening cursors, reading from them.

在某些天的测试我有没有错误报告,从我的用户,所以我觉得这个作品

After some days testing I've got no error reports from my users, so I think this works

在我的previous的工作,我在整个应用程序打开SQLite数据库很多次,这就是问题。

In my previous work I opened sqlite database many times across the application, that was the problem.

这篇关于那么多线程在Android的SQLite的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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