Android 为什么sqlite数据库的异步任务处理程序会立即触发postExecute
我有一个async类,它打开了一个sqlite数据库,如果是第一次运行,数据库将填充大约需要10秒才能完成的数据。即使仍有工作在后台完成,异步任务也会立即完成。我认为这与sqlite的oncreate方法有关,可能是在执行另一个异步任务之类的事情Android 为什么sqlite数据库的异步任务处理程序会立即触发postExecute,android,asynchronous,android-asynctask,android-sqlite,android-progressbar,Android,Asynchronous,Android Asynctask,Android Sqlite,Android Progressbar,我有一个async类,它打开了一个sqlite数据库,如果是第一次运行,数据库将填充大约需要10秒才能完成的数据。即使仍有工作在后台完成,异步任务也会立即完成。我认为这与sqlite的oncreate方法有关,可能是在执行另一个异步任务之类的事情 class BuildDatabase extends AsyncTask<Void, Void, Void> { ProgressDialog loading; @Override protected void
class BuildDatabase extends AsyncTask<Void, Void, Void> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
loading = ProgressDialog.show(ctx, "Please wait...", "Building database, this may take a few seconds", true);
}
@Override
protected Void doInBackground(Void... params) {
// open up the database, if its the first time running the data will
// be populated
db = new SQLiteWrapper(ctx); //takes 1-10 seconds
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.d(TAG,"post execute");
loading.dismiss();
}
}
如果您熟悉此类,则只调用onCreate,而数据库不存在。
如果在数据库构造函数中手动调用onCreate(),将显示进度对话框。
想法?
onCreate
只有在您实际打开数据库时才会被调用(而且数据库还不存在)
将this.getWritebleDatabase()
添加到SQLiteWrapper
构造函数中,如下所示:
public SQLiteWrapper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
ctx = context;
this.getWritableDatabase();
}
public SQLiteWrapper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
ctx = context;
this.getWritableDatabase();
}