7

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.)

This is what it says in the documentation.

Speaking network language, How long is 'a few seconds'?

My app will do the following, get an array list from DB, send it to server, get another list (json), send an okay that it received the list, parse the jsons, insert the list inside db, do some other processes in the db, update UI.. (the list can reach to 5000-1000 entries of object instances.)

Is usage of asynctask for such stuff a good idea? I also need to update the gui according to the results of the response from server.

If no, what other alternatives do I have?

Jeff
  • 12,555
  • 5
  • 33
  • 60
tony9099
  • 4,567
  • 9
  • 44
  • 73
  • a few is rarely more than one order of magnitude. what you describe seems quite long, meaning you will have users exiting the activity and expect the treatment to be finished in the background. an asynctask doesn't seem appropriate here. I +1 gunar and recommend and IntentService – njzk2 Sep 24 '13 at 15:40
  • what the heck "a few is rarely more than one order of magnitude" means mate ? this will be done silently on the background and the users wont leave the app.. @njzk2 – tony9099 Sep 24 '13 at 15:42
  • "a few is rarely more than one order of magnitude" => say around 10. I know that above that, users tend to exit the app one way or another, which will result in issues, from your thread being killed to the context of your activity being leaked. An intent service is really more appropriate in your case. – njzk2 Sep 24 '13 at 15:57

4 Answers4

4

I've already did something similar to you. I've downloaded a bunch of images (about 5000), saved it in the SD card and saved a row in a database. I've used Async Task with no issues and I see no problem using it. I recommend you to use a progress bar to show the user what the App is doing and with the option to cancel the task (AsyncTask provides a cancelation mechanism)

Augusto
  • 1,234
  • 1
  • 16
  • 35
  • 1
    okay @ACDias, looks promising,, so as far as I can tell, asynctask can handle the amount job I will be performing, as mine would be only text rather images. – tony9099 Sep 24 '13 at 15:40
3

See this answer: AsyncTask for long running operations

In summary, long-running AsyncTasks can create memory leaks due to its implementation. It can also cause issues with orientation changes.

Community
  • 1
  • 1
telkins
  • 10,440
  • 8
  • 52
  • 79
1

That's what I understand. In fact AsyncTask are mainly correlated with the UI.For example OnPostExecute is executed in the UI thread.

So in my understanding, you can use AsyncTask safely, if the user can reasonnably wait for the end of the operation.

The best way to know if your task fits the conditions, is to give a try.

HoodVinci
  • 656
  • 5
  • 7
0

Regarding Screen Orientation:

After reading the answers on this page, I realized that my app could not handle orientation changes while running AsyncTask.

Here is my solution to this problem:

@Override
protected void onPreExecute() {
    Device.lockOrientation((Activity)context);
    ...
}

@Override
protected void onPostExecute(List<Hydrant> hydrants) {
    Device.releaseOrientation((Activity)context);
    ...
}

The Device implementation:

public class Device {

    public static void lockOrientation(Activity activity) {
        Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int rotation = display.getRotation();
        int tempOrientation = activity.getResources().getConfiguration().orientation;
        int orientation = 0;
        switch(tempOrientation)
        {
        case Configuration.ORIENTATION_LANDSCAPE:
            if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            else
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
            break;
        case Configuration.ORIENTATION_PORTRAIT:
            if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270)
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            else
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
        }
        activity.setRequestedOrientation(orientation);
    }

    public static void releaseOrientation(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }

}

As AsyncTasks are only meant to run for a few seconds, this is an acceptable solution to me as this rarely will affect a user. Especially with something as a ProgressDialog giving a nice indication of how long she will have to wait.

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33