I have list of items to be syncronized with cloud. Let's say it has 10 items, so I have to make 10 HTTP requests to server. The question is: which approach should I use and why?
- foreach loop inside one async task
- 10 async tasks inside foreach loop?
I have list of items to be syncronized with cloud. Let's say it has 10 items, so I have to make 10 HTTP requests to server. The question is: which approach should I use and why?
Better would be to have one AsyncTask
and loop in in. And here is why:
All AsyncTasks
will by default on one background thread (with API 11+, eg Honeycomb), so your 10 tasks will still execute sequentially, but meanwhile will take much more memory. So it's really better to run only one AsyncTask
and if you need - publishProgress
on execution process.
Here is SO answer about having multiple AsyncTask
s.
Another approach is passing Executor
to your AsyncTask
, so it can break the limit of simultaneously runned AsyncTask
s. But still it can be really memory consuming.
Aslo you might want to read this Android Developers guide.