如果AsyncTask被取消,该怎么办?

盖伊

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

这是我尝试执行的操作:

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

我以为如果AsyncTask以任何方式被中断,“ onCancelled”将执行,但不会。如果以任何方式取消了AsyncTask所做的数据,我该怎么办?(例如,活动暂停,活动被破坏,互联网连接中断等)

anddev84

您的路线是正确的,但doInBackground()您还需要专门致电isCancelled()以检查是否已取消,然后从返回doInBackground()然后您的代码将正常工作。

请参阅AsyncTask文档“取消任务”

这是文档中的引用,以方便参考:

可以通过调用随时取消任务cancel(boolean)调用此方法将导致后续调用isCancelled()返回true。调用此方法后,将在返回后调用onCancelled(Object),而不是为了确保尽快取消任务,应始终定期检查的返回值,如果可能的话(例如在循环内)。onPostExecute(Object)doInBackground(Object[])isCancelled()doInBackground(Object[])

编辑:每个请求,一些示例代码:

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!
    }
}

希望有帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

如果我的发行证书过期,该怎么办?

来自分类Dev

如果我有几个重叠的QTimer,该怎么办

来自分类Dev

如果isAvailableForServiceType方法返回NO,该怎么办

来自分类Dev

该消息怎么办?

来自分类Dev

如果MIDL无法创建tlb,您该怎么办?

来自分类Dev

如果不等待任务该怎么办?

来自分类Dev

如果单击“保存文件”对话框上的“取消”按钮,该怎么办?

来自分类Dev

如果构造不需要括号,该怎么办?

来自分类Dev

如果“包装要求”过长,该怎么办?

来自分类Dev

如果没有上下文该怎么办?

来自分类Dev

当有人取消引用我的end()迭代器时该怎么办?

来自分类Dev

BDD结果:如果遇到错误该怎么办

来自分类Dev

如果遇到electron-dl,我该怎么办?

来自分类Dev

如果Pester断言失败,该如何指定该怎么办?

来自分类Dev

用户选择取消链接时该怎么办

来自分类Dev

在Arch上取消“ rm -rf / usr /”后该怎么办?

来自分类Dev

“如果!”怎么办?意思是?

来自分类Dev

如果bash停止回声,该怎么办?

来自分类Dev

如果AsyncTask被取消,该怎么办?

来自分类Dev

如果找不到302 URI,该怎么办?

来自分类Dev

如果div文字等于某些文字,该怎么办?

来自分类Dev

我该怎么办呢?该怎么办?

来自分类Dev

如果存在多个提交案例,我该怎么办

来自分类Dev

如果我想检查硒webdriver中是否存在该元素,该怎么办?

来自分类Dev

如果忘记启用EFI分区该怎么办?

来自分类Dev

如果建立连接失败,该怎么办?

来自分类Dev

如果没有上下文该怎么办?

来自分类Dev

如果要使用zabbix监视VoltDB,该怎么办?

来自分类Dev

如果短代码已经存在该怎么办

Related 相关文章

热门标签

归档