Reputation: 159
Is it a must that we have to initiate and execute asyctask from UI thread.Is it correct if i use asynctask for a webservice access(long running) from a non ui thread. sorry if my query is wrong.
In my app i have to run around 10 webservices and have to show the result on ui .i am confused which approach will be good asynctask,intentservice or creating thread for each webservice call and making it to run parallel.
Upvotes: 4
Views: 3467
Reputation: 2150
There are a few threading rules that must be followed for AsyncTask to work properly:
The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.
The task instance must be created on the UI thread.
execute(Params...) must be invoked on the UI thread.
Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.
The task can be executed only once (an exception will be thrown if a second execution is attempted.)
In my personal opinion, I would suggest using AsyncTask as it is highly optimized for running background tasks and exploit benefits like multicore processor.
Upvotes: 2
Reputation: 1550
You can go with either thread or asynctask. AsyncTask provide you methods to manage Ui changes methods before starting background task and after finishing backgroung task or getting its progress.
It depends on your architecture requirement.
Upvotes: 0
Reputation: 96
Yes, asynctask needs to be run from the UI thread.
I'd suggest to use multiple threads/runnables. You'll also need to implement a handler in your main activity class to listen to the responses from the threads.
Upvotes: 0