安卓:与进度对话框重DB操作 [英] Android: Heavy DB operations with progress dialog
问题描述
我已经习惯了<一href="http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#beginTransaction%28%29"相对=nofollow>下面上的AsyncTask的 doInBackground
方法快速插入到数据库成语:
I have used to following idiom on the doInBackground
method of an AsyncTask for quick inserts into the DB:
mDatabase.beginTransaction();
try {
//... do DB stuff
mDatabase.setTransactionSuccessful();
} finally {
mDatabase.endTransaction();
}
但是,如果我用这个成语,不显示我的进度对话框(放在在preExecute
法)。在另一方面,如果我替换上面的code与普通mDatabase.execSQL()语句,并得到显示进度对话框,但插入变得很慢。有什么办法,我可以有两全其美(即进度对话框,快速插入)的。谢谢!
However, if I use this idiom, my progress dialog (placed in the onPreExecute
method) is not displayed. On the other hand, if I replace the above code with regular mDatabase.execSQL() statements, the progress dialog does get displayed but inserts become very slow. Is there any way I can have the best of both worlds (i.e., progress dialog and fast insertions). Thanks!
注:调用BeginTransaction EXCLUSIVE模式运行(我不明白这是什么意思TBH)。这是什么原因?
Note: beginTransaction run in EXCLUSIVE mode (I don't get what that means tbh). Is this the cause?
推荐答案
我找到了答案。显然,这是我doInBackground这是问题之前在做什么。我已经定义了以下内容:
I found the answer. Apparently, it was what I was doing before doInBackground which was the problem. I had defined the following:
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgD = ProgressDialog.show(mContext, "", "Wait", true);
ih = new InsertHelper(mDatabase, "dictionary");
wordColumn = ih.getColumnIndex("word");
}
如果我将低于ProgressDialog.show的code到doInBackground的的BeginTransaction成语前,然后获取所示的对话框。我还是不明白为什么出现这种情况,但它就在那里。感谢您的帮助。
If I move the code below ProgressDialog.show into doInBackground before the beginTransaction idiom, then the dialog gets shown. I still don't get why this happens but there it is. Thanks for all your help.
这篇关于安卓:与进度对话框重DB操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!