Skip to content Skip to sidebar Skip to footer

Need Advice New AsyncTask Recursive Calling

I need advice is this solution acceptable and not cause overflow, I update data which read with AsyncTask, after AsyncTask finished I need to update again and again. Is this solu

Solution 1:

Yes, instantiating an AsyncTask multiple times is acceptable, example...

new DownloadFilesTask().execute(...);
...
new DownloadFilesTask().execute(...);

...is allowed.

You must not do something like the following though...

DownloadFilesTask myTask = new DownloadFilesTask();
myTask.execute(...); // This is OK
myTask.execute(...); // This will cause an exception

This is because it isn't legal to execute the same thread twice. In the first example using new repeatedly creates a new thread for doInBackground(...) but in the second example it is trying to re-use the previous one.


Solution 2:

By default AsyncTask handles it's own object pool automatically. So you dont have to worry about overflowing. I think it only allows 10 AsyncTasks to run at any one time by default, im not sure the exact number. And yes, like MisterSquonk said you have to have to create a new task each time.


Solution 3:

You just need something to make it stop, like an index or a condition, or it will keep running forever.

I did the following:

private ProgressDialog mProgressDialog;

private class FilesDownloadTask extends AsyncTask<Integer, Integer, Integer> {

    private Context context;

    public FilesDownloadTask(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(context);
            mProgressDialog.setMessage(getString(R.string.downloading));
            mProgressDialog.setIndeterminate(true);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
        }
    }

    @Override
    protected Integer doInBackground(Integer... index) {

        int i = index[0];

        if (i < fileList.length) {
            if (!new File(path[i]).exists())
                doDownload(urls[i], path[i]);
        publishProgress(i);
        }

        return i++;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(fileList.length);
        mProgressDialog.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(Integer nextIndex) {
        if (index < fileList.length)
            new FilesDownloadTask(context).execute((Integer) nextIndex);
        else
            mProgressDialog.dismiss();
    }

    @Override
    protected void onCancelled() {
        mProgressDialog.dismiss();
        super.onCancelled();
    }
}

Post a Comment for "Need Advice New AsyncTask Recursive Calling"