Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call multiple AsyncTask at a time not working in Android

I want to call multiple AsynTask. It was not working so I google for some help. I found that If I use AsyncTask.THREAD_POOL_EXECUTOR then it will work.

But It is not working.

I am calling it as below:

new GetCategory().execute();
new GetArea().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

Please help me regarding this issue. I want to call it at a time or it is also ok if GetArea() call after GetCategory(). I am not getting error. Application may be doing too much work.
Logcat:

03-13 21:21:43.134: I/Choreographer(1222): Skipped 39 frames!  The application may be doing too much work on its main thread.
03-13 21:21:43.394: I/Choreographer(1222): Skipped 43 frames!  The application may be doing too much work on its main thread.
03-13 21:21:43.634: I/Choreographer(1222): Skipped 36 frames!  The application may be doing too much work on its main thread.
03-13 21:21:43.784: I/Choreographer(1222): Skipped 38 frames!  The application may be doing too much work on its main thread.
03-13 21:21:44.004: I/Choreographer(1222): Skipped 56 frames!  The application may be doing too much work on its main thread.
03-13 21:21:44.164: I/Choreographer(1222): Skipped 36 frames!  The application may be doing too much work on its main thread.
03-13 21:21:45.495: I/Choreographer(1222): Skipped 33 frames!  The application may be doing too much work on its main thread.
03-13 21:21:45.765: I/Choreographer(1222): Skipped 43 frames!  The application may be doing too much work on its main thread.
03-13 21:21:46.005: I/Choreographer(1222): Skipped 36 frames!  The application may be doing too much work on its main thread.
03-13 21:21:46.235: I/Choreographer(1222): Skipped 44 frames!  The application may be doing too much work on its main thread.
03-13 21:21:46.525: I/Choreographer(1222): Skipped 48 frames!  The application may be doing too much work on its main thread.
03-13 21:21:46.855: I/Choreographer(1222): Skipped 39 frames!  The application may be doing too much work on its main thread.
03-13 21:21:47.005: I/Choreographer(1222): Skipped 39 frames!  The application may be doing too much work on its main thread.
03-13 21:21:47.156: I/Choreographer(1222): Skipped 37 frames!  The application may be doing too much work on its main thread.
03-13 21:21:47.306: I/Choreographer(1222): Skipped 38 frames!  The application may be doing too much work on its main thread.

I am binding Spinner on both AsynTask.
My Code :

        Area[] areaList = gson.fromJson(result, Area[].class);

        List<Area> lList = Arrays.asList(areaList);
        Area objArea;

        areaId.add("0");
        areaName.add("Select Area");

        for (int i = 0; i < lList.size(); i++) {
            objArea = lList.get(i);
            areaName.add(objArea.getAreaName());
            areaId.add(objArea.getAreaId());
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        getActivity(), android.R.layout.simple_dropdown_item_1line, areaName);
                ((Spinner) spnCEArea).setAdapter(adapter);
like image 250
Jeeten Parmar Avatar asked Mar 13 '14 15:03

Jeeten Parmar


2 Answers

The reason why you can't call two .execute() methods on AsyncTask within the same project at the same time is the modification that Android introduced since Gingerbread. Up until this version, you were able to call .execute() as many times you needed as each of them were run in a separate Thread.

But as of this version, they're called sequentially so if one is executing, the other one won't start if you just call .execute().

So basically all you need to do is to check the version of the device you're running the AsyncTask on and run one or other command depending at it.

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR1)
  your_asynctask.execute(your_params);
else
  your_asynctask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, your_params);

More info here.

By the way, the The application may be doing too much work on its main thread. lines are not related to the AsyncTask. These tasks are being run in background and if you're doing the work within your doInBackground() method, it's not the issue.

A slightly different case might be if you're doing some work in your onPostExecute() task, as this might be related to your main UI, but we can't tell you more without knowing the code you're running.

like image 179
nKn Avatar answered Oct 10 '22 01:10

nKn


Edited:

@Override
public void doInBackground(params.....){

        //Your other code goes here

            Area[] areaList = gson.fromJson(result, Area[].class);

            List<Area> lList = Arrays.asList(areaList);
            Area objArea;

            areaId.add("0");
            areaName.add("Select Area");

            for (int i = 0; i < lList.size(); i++) {
                objArea = lList.get(i);
                areaName.add(objArea.getAreaName());
                areaId.add(objArea.getAreaId());
            }

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                            getActivity(), android.R.layout.simple_dropdown_item_1line, areaName);

}

@Override
public void onPostExecute(Params..){


                ((Spinner) spnCEArea).setAdapter(adapter);
}

In this way, you place all your workload in doInBackground and just fitting adapter to listView in onPostExecute. This is the right way to do. And I am sure it solves the issue.

like image 24
Dipendra Avatar answered Oct 10 '22 02:10

Dipendra