Difference between CursorLoader and AsyncTaskLoader
Asked Answered
K

4

18

Why should I use AsyncTaskLoader, and when should I prefer CursorLoader and vice-versa ?

In official page, the App-listing example is shown, this uses AsyncTaskLoader instead of CursorLoader.

What are the advantages and disadvantages of these two? I read somewhere about the CursorLoader not taking care about the content change (in sqlite).

Thank you!

Kinata answered 25/2, 2015 at 3:32 Comment(0)
E
23

AsyncTaskLoader is a abstract Loader that provides an AsyncTask to do the work.So you usually extend the AsyncTaskLoader to create your very own custom loader.The key difference between using a AsyncTask and using a AsyncTaskloader is that configuration changes (such as orientation change) do not affect AsyncTaskLoader and its processes since AsyncTaskLoader has its own lifecycle ;while the configuration changes affect the AsyncTask adversely since it is connected to host activity's lifecycle.

CursorLoader is a loader that queries the ContentResolver and returns a Cursor.This class implements the Loader protocol in a standard way for querying cursors.It is nothing but a AsyncTaskLoader.

In short,you can use AsyncTaskLoader when you have to create a custom loader by extending AsyncTaskLoader< D > where D="anything_you_want_to_load". And you use CursorLoader when you have to implement a loader which loads Cursor (usually used when you have load data from a database).

Edgerton answered 19/6, 2015 at 7:52 Comment(0)
D
4

One thing worth noting is that CursorLoader is a concrete implementation of AsyncTaskLoader<D>. Basically, CursorLoader can be thought of as an AsyncTaskLoader specifically made to deal with Cursor objects.

Danford answered 18/2, 2016 at 1:43 Comment(0)
P
1

The AsyncTaskLoader is One particular subclass of Loaders is of interest. This class performs the same function as the AsyncTask, but a bit better. There are a few issues with using AsyncTasks, though:

Configuration changes can mess things up Pausing an activity doesn’t pause the AsyncTask A fair amount of boilerplate code (which means more possible errors)

It can handle Activity configuration changes more easily, and it behaves within the life cycles of Fragments and Activities. The nice thing is that the AsyncTaskLoader can be used in any situation that the AsyncTask is being used. Anytime data needs to be loaded into memory for the Activity/Fragment to handle, The AsyncTaskLoader can do the job better.

The beauty of the Loader is that it handles some of the ‘gotchas’ that usually are missed when using the AsyncTask. Mainly, it handles activity configuration changes (IE when the user rotates the screen).

On the other hand, CursorLoader really shine when using Cursors within Android to pull data. The Loader class does an excellent job of updating the Cursor information (and in turn, the UI) whenever the underlying data changes. This is immensely helpful when information changes often and you don’t want to interrupt the UI, and whatever the user is currently doing, just to display some new information.

Polyurethane answered 25/2, 2015 at 4:13 Comment(1)
Waiting for someone to confirmKinata
M
1

Differences in CursorLoader and AsyncTaskLoader are

  • AsyncTaskLoader can be used to return custom objects but CursorLoader returns only Cursor
  • CursorLoader can receive getContext().getContentResolver().notifyChange(uri, null); but AsyncTaskLoader does not receive.
Mendicity answered 8/12, 2015 at 4:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.