If you create new Async tasks for each different call or use the same one

So I have an application that will create an HTTP message / multipe recipient

eg. Input, getThisData, getThatData, sendThis, sendThat

Is it better to have a separate AsyncTask to handle each of them.

Or one aync task and handle them differently with the switch in onPostExecute and doInBackground

Greetings

+3
source share
4 answers

Short answer: yes, you have to create a new AsncTask for each call.

And if you're interested, the long answer is:

According to Android Asynctask documentation,

  • AsyncTask's goal is to take care of stream management for you, and you should not worry about streaming mechanisms.
  • The Android platform supports a thread pool for managing asynchronous operations. AsyncTasks are like consumables. A task can be executed only once (an exception will be thrown when trying to perform a second execution).

Happy asynchronous coding! :-)

+7
source

It depends on whether the tasks are independent of each other or whether they are related to each other. If you are independent, you can handle this through the same asin. For example, if you need data from your input response and pass this value for this task, you better use a separate asynchronous mode. Make login separate async, getthis, get lthat sendthis sendthat can be in one asynchronous mode.

0
source

You probably want to separate them, especially if their functionality in the pre / post execution is different. Try organizing the code into logical blocks. For example, if you have an Async task for logging in and an Async task, for example, to load a lot of document data via JSON, they will need separate Async tasks.

However, let's say you have two separate API calls that return similar or partially the same data - if one returned the file details (name, size) and the other returned the same data, but was a file revision - you can switch them in the same ASYNC, because the post post will execute the same (or partially the same) code to retrieve data from JSON.

0
source

If you want to create one instance of AsyncTask and execute () it several times, no:
Run AsyncTask several times

If you ask about development: do I need to write one class to get different data? It really depends on your circumstances:
If this HTTP call needs to be consistent, you can put them in a single AsyncTask class.
If they have a lot in common, just specify different URIs, you can write a call method (String uri) and call this method in your AsyncTask. In this case, I think one AsyncTask is enough.

0
source

Source: https://habr.com/ru/post/1502134/


All Articles