0

Context : networking(http request and response).

My question has two parts - one about volley and one about AsyncTask.

I'm learning to implement httpConnection using volley and AsyncTask, so please bear with me.

My question is, in what order the callbacks are called, after getting response from network? (In the case of volley and AsyncTask)

Are callbacks executed sequentially?

Consider this scenario,

I have 2 AsyncTasks. After getting response form network at same time, in what order callback will be called in postExecute()? Will both callbacks to UI thread will occur simultaneously as 2 Asynctasks are in seperate threads? or will they be executed sequentially?

I have read these SO posts,

can-i-do-a-synchronous-request-with-volley

multiple-asynctasks-onpostexecute-order

running-multiple-asynctasks-at-the-same-time-not-possible

how-to-manage-multiple-async-tasks-efficiently-in-android

how-to-synchronize-two-async-task

Thanks

Edit : Consider this code of AsyncTask implementation,

public class AsyncTaskImplementation extends AsyncTask<String, Void, String> {
    private final interface appAsyncTaskInterface;
 
    public interface responseInterface {
        void onSuccessHttpResponse(String resultJsonObject);
        void onFailedHttpResponse(String failedMessage);
    }

    public AsyncTaskImplementation(){
        //initialise
    }

    @Override
    protected String doInBackground(String... params) {
        //executeHttpRequest and return
    }

    @Override
    protected void onPreExecute() {
        //pre execute stuff like connection checking
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (result != null&& !result.equals("")) {
            responseInterface.onSuccessResponse(result);
        } else {
            responseInterface.onFailedinterface("failed");
        }
    }
}

Question wrt to above code :

Consider two AsyncTaskImplementation objects, A and B created in a UI thread.

Now A and B will execute a http request (assume the code is implemented).

After that they will return response through interface responseInterface.

So my question is regarding this. Will A and B simultaneously invoke callback methods in UI thread through responseInterface?

Should I use synchronized to make sure that callbacks are synchronized? Or AsyncTask ensures that callbacks are invoked sequentially?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
mrtpk
  • 1,398
  • 4
  • 18
  • 38

0 Answers0