Ale*_*Ale 5 database blocking ui-thread android-asynctask
我正在ondoInBackground内的方法中执行数据库操作。AsyncTaskAndroid
由于某种原因,UI该操作在 5-6 秒内被阻塞。
这对我来说没有任何意义, 中的操作doInBackground不应该在 中执行UI Thread,对吗?
这是我的代码:
private class CountItems extends AsyncTask<String, Void, Integer> {
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(context);
dialog.setCancelable(false);
dialog.setMessage(getString(R.string.asynTask_loading));
dialog.show();
}
@Override
protected Integer doInBackground(String... params) {
// This operation takes 5-6 seconds.
return app.databaseSession().getMyObjectDao().count(selectedProject, filter, null, false);
}
@Override
protected void onPostExecute(Integer result) {
counterTextView.setText(String.valueOf(result));
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我做了一个测试。如果我将 a 放在方法Thread.sleep()内部doInBackground,它将以不同的方式执行,Thread而不会阻塞UI.
像这样:
@Override
protected Integer doInBackground(String... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?提前致谢。