Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between CursorLoader and AsyncTaskLoader

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!

like image 415
Nabin Avatar asked Feb 25 '15 03:02

Nabin


1 Answers

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).

like image 115
Tejas Avatar answered Oct 05 '22 13:10

Tejas