devqmr
devqmr

Reputation: 501

using Single AsyncTask Vs. multiple AsyncTask for multiple different request from network

I have question about AsyncTask I try to open 10 request to Internet for 10 Json files, so I read it and save it to user device _this file must be separated due to differences in the data, including.

So,what is better to put every single request in single AsyncTask. thus,I become have 10 AsyncTask,Or call all this 10 request in one AsyncTask ?? and why you prefer this way for other? thank you all.

Upvotes: 1

Views: 228

Answers (3)

fab
fab

Reputation: 790

No need to reinvent the wheel :)

Try to use volley android framework from Square to parallelize your tasks

Hope this help you!

Upvotes: 2

marcinj
marcinj

Reputation: 49976

I would put it in single AsyncTask, this is actually because AsyncTasks are difficult to manage in Activites, once your Activity rotates or user presses Home button - it begins its lifecycle - which can end in onCreate - which means your activity might get destroyed and then recreated. Now if you need to somehow report your results to Activity, then your are in trouble. With one AsyncTask its a lot easier.

If your AsyncTasks are of fire and forget kind, then maybe issuing 10 AsyncTasks wont be that much bad, but then you can use your own Executor, no need to block AsyncTask thread pool for non-gui tasks (AsyncTasks since honeycomb are serialized).

Upvotes: 1

Morgan LaVigne
Morgan LaVigne

Reputation: 153

As a matter of form I like to put them in separate Async Tasks because they might complete at different times, and rather than programming around that, it's best to use the system that's designed for it.

The exception is when spawning a ton of threads might really slow everything down. But 10 is not that big a deal. So yeah, use separate tasks, IMHO

Upvotes: 1

Related Questions