gpt4 book ai didi

android - 如果 AsyncTask 被取消,做些什么?

转载 作者:搜寻专家 更新时间:2023-11-01 08:01:04 27 4
gpt4 key购买 nike

我正在使用 AsyncTask 填充 SQLite 数据库。我正在从某个网页下载数据并将其放入 SQLite 表中。问题是,我要么下载 100% 的数据,要么什么都不下载。因此,如果 AsyncTask 由于某种原因被中断,我想删除到目前为止已下载的所有数据。

我是这样尝试的:

@Override
protected void onCancelled() {
super.onCancelled();
dbHandler.deleteFromDatabase(razred);
Log.i("TAG", "AsyncTask cancelled");
}

我认为如果 AsyncTask 以任何方式中断,“onCancelled”将执行,但事实并非如此。如果以任何方式取消,我该怎么做才能删除使用 AsyncTask 生成的数据? (例如 Activity 暂停、 Activity 被破坏、互联网连接中断等)

最佳答案

你在正确的轨道上,但是在你的 doInBackground() 中你还需要专门调用 isCancelled() 来检查它是否被取消,然后从 doInBackground()。然后您的代码将正常工作。

引用AsyncTask documentation对于“取消任务”

为了便于引用,这里引用了文档中的内容:

A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)

编辑:根据请求,一些示例代码:

private class MyAsyncTask extends AsyncTask<Void,Void,Void> {

private SQLiteDatabase db;

@Override
protected void onPreExecute() {
// any kind of initialization or setup needed before the
// background thread kicks off. remember: this is still on
// on the main (UI) thread

// since youre doing DB I/O, Ill make believe Im initializing the DB here
db = DatabaseHelper.getInstance(MainActvity.this).getWritableDatabase();
}

/*
* The background thread to do your disk and network I/O. If you need
* to pass in any parameters, this is the first Void in the template
*/
@Override
protected Void doInBackground(Void... params) {

// other stuff you need to do in the background. Since you want an
// all-or-nothing type thing, we will use a transaction to manually
// control the db
db.beginTransaction();
try {
// do network I/O to retrieve what you need and then write to DB.
...
... // if theres a loop in here somewhere when reading the data, check !isCancelled() as part of the condition or as one of the first statements and then break
...
db.setTransactionSuccessful(); // assuming everything works, need to set
// this successful here at the end of the try
} catch (InterruptedException ie) { // or some other exception
cancel(true); // heres where you can call cancel() if youve been interrupted
} catch (IOException ioe) { // if your network connection has problems
cancel(true);
} finally {
db.endTransaction();
// other cleanup, like closing the HTTP connection...
// no need to close the DB if you implement it properly
}
return null; // if there was some return value, that would go here
}

@Override
protected void onCancelled(Void result) {
// depending on how you implement doInBackground(), you may not even need this,
// unless you have a lot of other "state" you need to reset aside from the DB transaction
}

@Override
protected void onPostExecute(Void result) {
// any other items to do on main (UI) thread after doInBackground() finishes
// remember, this only gets called if cancel() is not called!
}
}

希望对您有所帮助!

关于android - 如果 AsyncTask 被取消,做些什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21173459/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com