Andrew Stef
Andrew Stef

Reputation: 61

Is there any scenario where using AsyncTask is better than AsyncTaskLoader?

I am currently studying about Loaders and about how they could overcome changes such as screen orientation during application lifecycle, and so far, from what I have read, AsyncTaskLoader does the same job as AsyncTask and even better. Therefore, should not AsyncTask be considered obsolete or does it provide the developers with some hidden advantages?

Upvotes: 1

Views: 161

Answers (2)

CommonsWare
CommonsWare

Reputation: 1006674

I am currently studying about Loaders and about how they could overcome changes such as screen orientation during application lifecycle

The Architecture Components' support for view-models and LiveData is Google's current direction for addressing the problems that loaders tried to address.

AsyncTaskLoader does the same job as AsyncTask and even better

Note that AsyncTaskLoader uses an AsyncTask.

should not AsyncTask be considered obsolete

Yes, insofar as we have other patterns and libraries to use (e.g., RxJava, LiveData). That being said, AsyncTask, used correctly, is an OK option. The challenge is in using it correctly (e.g., from a retained fragment, with care to avoid interacting with the hosting activity on a background thread).

does it provide the developers with some hidden advantages?

Your argument seems to be "an ocean liner has more features than does a rowboat, so shouldn't we consider rowboats to be obsolete?". Ocean liners have their costs, and ocean liners cannot do everything that a rowboat can (e.g., travel in shallow water, be towed behind a truck).

Loaders were designed — to the extent that they ever had a "design" — to:

  • load data in the background, typically via an AsyncTask
  • retain that data across configuration changes
  • automatically deliver updates when the requested data happens to change

Not everything needs that. For example, the loader pattern targets read operations (where we actually "load" data), but it does not really help with write operations (where we are changing the data). Yet we still want to do write operations asynchronously and find out about the results even if we undergo a configuration change. You can squeeze write operations into loaders, but it is not a natural fit. Using an AsyncTask or something else, instead of a loader, would be more natural.

Upvotes: 2

Alifyz Pires
Alifyz Pires

Reputation: 73

Think this way,

The AsyncTaskLoader can be used at any place where the AsyncTask is. The main advantage is the ability to persist between the lifecycles.

If you use an AsyncTask and call a Network Operations to get some data from the Internet and the User rotates the phone, your AsyncTask will have to start the task again to grab the data, and this could be potentially dangerous to your application because you could have a memory leak.

So, in any case, Loaders are an evolution of the AsyncTask, they are basically improved AsyncTasks.

I believe that the AsyncTask is still alive because when you are performing some simple task in the Background Thread you can do this more simply using an AsyncTask with an anonymous inner class, and deliver the results right away to the UI Thread.

Upvotes: 1

Related Questions