MainstreamDeveloper00
MainstreamDeveloper00

Reputation: 8556

What is the order of onPostExecute(Result) methods execution?Can i change it?

I'm implementing quite a simple task - user authorization.All network operations I've implemented via AsyncTask. And I have a problem. I have two AsyncTasks - one it is authorization which calls more low level and common for all network tasks operation (JSON parsing, REST realization etc.) which also is implemented via AsyncTask. And I have a problem: onPostExecute methods of these two operations are called in a wrong order.

So the question - can I someway set the order of their execution, or may be you give me some advice how to handle all this in a right way.But don't suggest me to refuse AsyncTask and use something else. I want a right solution with AsyncTask. Thanks in advance.

Upvotes: 2

Views: 170

Answers (2)

Ted Hopp
Ted Hopp

Reputation: 234847

It sounds like you should have a single AsyncTask for the high-level background operation. The low-level network activities should be invoked directly in the high-level doInBackground. By having two AsyncTasks running, you lose the ability to easily coordinate the timing of their individual work.

If for some reason you need to independently run the lower-level operations in an AsyncTask (that is, for purposes other than authorization), then that AsyncTask should also directly invoke the low-level network activities from its own doInBackground; however, the low-level activities should still be separate from any AsyncTask.

Upvotes: 1

Talha
Talha

Reputation: 12717

Run your 2. asycTask in 1. fisrt asycTask onPostExecute method. By the way you can set its order yourself.

like below,

 private class task1 extends AsyncTask<URL, Integer, Long> {

     protected int doInBackground(URL... urls) {
         // do your stuf
         return 1;
     }


     protected void onPostExecute(int result) {
           // first task finished and second task starts
           new task2().execute();
     }
 }


private class task2 extends AsyncTask<URL, Integer, Long> {

     protected int doInBackground(URL... urls) {
     // do your stuf
         return 1;
     }


     protected void onPostExecute(int result) {
        // do your stuf
     }
 }

Upvotes: 0

Related Questions