RapsFan1981
RapsFan1981

Reputation: 1197

Do I need to use multiple AsyncTask subclasses?

My app has FTP functionality and now I want to implement a ProgressDialog for the various operations (Connect, download, upload, file list etc.) I found this snippet here on stack and it seemed like a good starting point.

public class LoadData extends AsyncTask<Void, Void, Void> {
        ProgressDialog progressDialog;
        //declare other objects as per your need
        @Override
        protected void onPreExecute()
        {
            progressDialog= ProgressDialog.show(FTPConnector.this, "Please wait for ","Process Description Text", true);

            //do initialization of required objects objects here                
        };      
        @Override
        protected Void doInBackground(Void... params)
        {   

             //do loading operation here  
            return null;
        }       
        @Override
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);
            progressDialog.dismiss();
        };
     }

I've implemented it as a subclass of my FTPConnector class which contains the various methods for connect, download, upload, listfiles,rename, delete. Do I need to create a subclass of ASyncTask for each FTP operation? If not what can I do?

Upvotes: 0

Views: 342

Answers (3)

Eugene
Eugene

Reputation: 530

Using AsyncTask for multiple transaction will work, but this is not proper solution. As written before: it could cause problems. I suggest you to take a look at this video(it's about REST client, but described patterns will work for you too). Follow these rules:

  • Perform transaction in a separate thread
  • Every thread must be started from a Service
  • To retain state of transaction use database(don't keep states in a memory)
  • Use SyncAdapter. Not sure that it works for you(it's depends on your needs)

Upvotes: 1

Rarw
Rarw

Reputation: 7663

Firs of all, I was under the impression that using AsyncTask as a subclass of activity was disfavored since the task would retain a reference to the activity that started it which could cause problems. I've generally used AsyncTask as its own class and passed in the context of the activity that executed it. You should look into that.

Second you could make different tasks for each server transaction. I guess it depends how many their are or how complicated your application is. Other than that you could use one class with a switch statement in the doInBackground method and pass in a string tag as one of the varargs (assuming your task takes only strings as initial arguments). This just seems cumbersome and ugly.

I'd probably just make one task for each transaction.

Upvotes: 2

codeMagic
codeMagic

Reputation: 44571

Do I need to create a subclass of ASyncTask for each FTP operation?

Not necessarily but that will depend on the logic that you use inside your AsyncTask methods. If you make them to handle the function that operation needs then you will be ok. But depending on the logic for each operation, it may be easier to control if you create a separate class for each.

However, note that you will need to create a new instance each time you need to call execute(), according to the Docs

The task can be executed only once (an exception will be thrown if a second execution is attempted.)

Upvotes: 2

Related Questions