1

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?

  1. foreach loop inside one async task
  2. 10 async tasks inside foreach loop?
Federricco
  • 129
  • 1
  • 7

1 Answers1

2

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 AsyncTasks.

Another approach is passing Executor to your AsyncTask, so it can break the limit of simultaneously runned AsyncTasks. But still it can be really memory consuming.

Aslo you might want to read this Android Developers guide.

Ekalips
  • 1,473
  • 11
  • 20
  • Thanks for the explanation and links. Additional reason to use one async task just came to my mind: In case of problems with connection I have to handle as many connection errors as tasks I've lounched. – Federricco Jun 28 '17 at 04:47