I have an existing code which has an Async Task used for some request-response. In the post execute method it sets the parsed data into some db.
Now I need to modify this code in such a way that at app launch, the data gets downloaded one by one. i.e. I need to execute task A, then on its full completion (even the data is set) I need to start task B and so on for around 12 tasks.
Note: I need to finish the "post execute" as well before starting the next task.
I am not sure how to do this. Please suggest.
This one gets a bit messy..
Configuring a AsyncTask with SERIAL_EXECUTOR will indeed force serial execution of background logic, that is, the logic contained within the doInBackground() calls.
SERIAL_EXECUTOR makes no guarantees as to when the onPostExecute() will be invoked.
Executing a set of AsyncTask in SERIAL_EXECUTOR mode may result in onPostExecute() of task A being executed after the doInBackground() of task B.
If the latter demand is not crucial to your system, just use SERIAL_EXECUTOR and be happy.
But, if it is, you will need to change your architecture in a way that will force such demand.
one way of doing so will be as follows:
That is: you will wait for the UI update of one operation to complete before starting the next one.
Another way, which I would probably prefer, would be to manage th entire task flow within a single thread that internally perform att background tasks A -> B -> C .... and, in between, issues UI update commands and waits on a ConditionVariable for these tasks to complete
Hope it helps.
You can do it with
myAsyncTask.executeOnExecutor (SERIAL_EXECUTOR)
Now the exection order will be a -> b -> c;
The implementation depends on your requirement.
If after every call you need to update your UI you can download and save data in
doInBackground()
and the update inonPostExecute()
.If you wanna update your UI just once then download all the data inside a loop inside your
doInBackground()
and also save your data in db there only and finally call youronPostExecute()
just once.If it has nothing to do with updating the UI then you can simply use
Thread
.NOTE :
AsyncTask
order of execution will be serial only aboveHoneyComb
below that it would be parallel.