This is what I'm trying to do:
doInBackground, some data is downloaded.onPostExecute, the downloaded data is processed and if the data isn't what it has to be, I want the AsyncTask to restart itself from the onPostExecute.My question is: is it bad practice to call new MyTask().execute(); from the Async itself to make it restart? Is it okay to do that or is there a better way?
Like your icon Merlin. I'm sure there's a dragon out there too.
You probably just don't want an AsyncTask for that (AsyncTask wasn't intended to be a catch-all solution to concurrency in Android- even less Looper or the like, which is for dispatching things of a completely different nature. Oh and not new Thread with a Looper- you'd just be redesigning something that's readily available in Java and is widely regarded as one of the most reliable and desirable features of the language!)
Take a look at an executor service (docs, nice android docs with really good example similar to your purpose!) or even a scheduled executor service (docs, android docs). java.util.concurrent is your best of friends!
This way you can create a task and submit it more than once without having to call new.
Just another question: onPostExecute runs on the UI thread. From the android docs:
onPostExecute(Result), invoked on the UI thread after the background computation finishes
You say "the downloaded data is processed". Does this require touching UI elements? Shouldn't this be asynchronous too, therefore not in onPostExecute?
Best of luck!
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