Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynctask vs Thread vs Services vs Loader

I got slightly confused about the differences between Asynctask, Thread, Service, Loader in Android.

I know how it works. But i still don't understand what and when should i use.

I work with Android for 3 years, and generally still use AsyncTask for all background tasks (and sometimes Thread). But many people say that "Asynctask is outdated", and don't recommend to use them. Also they recommend to use robospice or Volley.

So, is AsyncTask really so bad and i should use framework for networking tasks? And what should i use for background (not networking) task?

like image 712
Suvitruf - Andrei Apanasik Avatar asked Jul 09 '14 10:07

Suvitruf - Andrei Apanasik


People also ask

What is difference between service thread and AsyncTask?

service is like activity long time consuming task but Async task allows us to perform long/background operations and show its result on the UI thread without having to manipulate threads.

What is the loader equivalent of AsyncTask?

AsyncTaskLoader is the loader equivalent of AsyncTask .

What is the difference between thread and service?

Service : is a component of android which performs long running operation in background, mostly with out having UI. Thread : is a O.S level feature that allow you to do some operation in the background.

What is the difference between AsyncTask and AsyncTaskLoader?

AsyncTask will be re-executed as background thread again, and previous background thread processing was just be redundant and zombie. AsyncTaskLoader will be just re-used basing on Loader ID that registered in Loader Manager before, so avoid re-executing network transaction.


2 Answers

Threads: Same as Java threads, use it to do heavy operations but you have to manage it on your own and it can also cause synchronization problems and you can't update UI from it until you are running it on the UI thread.

AsyncTask: A great threading library available in Android for doing background task. It is managed by the android OS itself, you can update the UI from it. It runs in parallel or serially depending upon the version of android. It can be messy sometimes to use it as in the cases of orientation changes and now for making network calls you can use volley which is better than AsyncTask. AsyncTasks do not bother about their parent activity is running or not and it can be quite tedious to sometimes cancel that. So, I would suggest you if you are using AsyncTask to make rest API calls better use RETROFIT or VOLLEY and if you choose RETROFIT between the two I would recommend you to have a look at PICASSO another awesome library from square for image loading.

Service: For doing long-term background tasks you should use services. You can bound the services to your activity if you need. You can define that they run in the same thread or a different thread and you need to declare it in manifest or you can use IntentService - a variant of service which runs in its own thread but be careful before using it, don't use it for long running tasks. It's a single time operator. If you are going to use service evaluate the case that which one suits your requirement better a normal Service or IntentService

Loaders: this is same as AsyncTask in many ways, it is advised to use loaders with the fragments and it solves the orientation problem of the asynctasks.

If you have already moved to kotlin I would suggest you to have a look at Coroutines.These are very lightweight and quite effective for threading and provide you a lot of control over the lifecycle. I hope this helped.

like image 141
Pramod Yadav Avatar answered Sep 22 '22 04:09

Pramod Yadav


AysncTasks are not 'outdated' as much as they are incomplete. Among other things async tasks do not bother if their parent activity is currently running or not. For the same reason why you include checks for verify the context to be null or not. Besides, unless you are using your own Thread Pool Executor these tasks execute serially.

Volley tries to fill in those gaps, mainly concerning syncing up with the main thread and thread pooling. It behaves optimal if you wish to do stuff that requires average network requests; like some meta data list and images(picture the youtube app requests and facebook app requests for posts).

Typically few advantages of Volley are as follows

  1. It keeps the worker thread informed about the activity(Main thread)
  2. Easier resource prioritization you could provide priority to your download requests. A typical scenario would involve you giving priority to text over image.
  3. Effective request cache and memory management.
  4. Extensible
  5. It provides you an option to discard your request in-case your activity was shutdown or restarted.
  6. Simpler patterns for data retrieval as opposed to AsyncTasks.

Volley fares badly when it comes to streaming requests/video as mentioned at Google I/O.

I'm not exactly aware of robospice. Ps: If you have time on your hand see https://www.youtube.com/watch?v=yhv8l9F44qo

Here's a further read if you wish to go into other libraries with benchmarks for the same. Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley

like image 30
humblerookie Avatar answered Sep 22 '22 04:09

humblerookie