I know that Similar questions have been asked before but the solution to those questions will not work for me. The situation is that I have an Array of objects that are each getting sent to uploaded to the server via an asynctask. I need to send each one separately but each object in the list uses the same asynctask so calling task.execute in the onPostExecute() is not an option since that would cause it to infinitely call itself. I have tried something like so
for(each item in list){
task.execute();
while(task.getStatus() != android.os.AsyncTask.Status.FINISHED){
//spin here and wait
}
}
But when doing this it just spins for ever and never leaves the while loop. Any suggestions are greatly appreciated
From AsyncTask docs:
Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.
so if you are not >= HONEYCOMB, then maybe switch to use ExecutorService:
ExecutorService executor = Executors.newSingleThreadExecutor();
Why don't you change your architecture: Make the uploading async task take an array of files to upload and just loop through them?
If you really want to stay this way, just use a lock object.
Something like:
class MyAsyncClass extends AsyncTask{
static Object lock = new Object();
.......
void doWork(){
synchronized(lock) {
//do stuff
}
}
}
EDIT: I should point out that this does not ensure you, that the doWork() methods are executed in the order, they were called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With