dooplaye
dooplaye

Reputation: 1029

What's practical purpose of cursorLoader?

I saw some articles about CursorLoader like this, but I still don't understand the particular purpose of using it.

I developed apps with SQL and cursor retrieving. The point is it was very fast. I queried and parsed cursor with >500 record and 8 columns by a few millisecond. So didn't notice any delay event on old phones. So why do I need to use it?

Upvotes: 0

Views: 3554

Answers (4)

Vartika Sharma
Vartika Sharma

Reputation: 134

A CursorLoader runs a asynchronous query in the background against a ContentProvider and then it returns the result back to the Activity or the Fragment from where it is called. The main advantage is that it helps the user to interact with Activity or Fragment while the query is still running in the background.

Upvotes: 1

naXa stands with Ukraine
naXa stands with Ukraine

Reputation: 37993

Use the CursorLoader from Support Library to run asynchronous queries in the background. In this way, you ensure that data loading does not cause “Application Not Responding” messages.

Upvotes: 0

Yash Sampat
Yash Sampat

Reputation: 30611

A CursorLoader is used ostensibly to query a ContentProvider with LoaderManager.LoaderCallbacks<Cursor>.

There are two things that you need to keep in mind for understanding the CursorLoader:

  • It loads data on a separate thread.

  • It monitors the underlying data source for updates, re-querying when changes are detected.

Now coming to the LoaderManager. Simply stated, the LoaderManager is responsible for managing one or more Loaders associated with an Activity or Fragment. Each Activity and each Fragment has exactly one LoaderManager instance that is in charge of starting, stopping, retaining, restarting, and destroying its Loaders. These events are sometimes initiated directly by the client, by calling initLoader(), restartLoader(), or destroyLoader(). Just as often, however, these events are triggered by major Activity/Fragment lifecycle events. For example, when an Activity is destroyed, the Activity instructs its LoaderManager to destroy and close its Loaders (as well as any resources associated with them, such as a Cursor).

The LoaderManager does not know how data is loaded, nor does it need to. Rather, the LoaderManager instructs its Loaders when to start/stop/reset their load, retaining their state across configuration changes and providing a simple interface for delivering results back to the client.

So you see, all this is not easily possible when you use a simple AsyncTask and query an SQLite database. This is why the framework provides CursorLoader and LoaderManager:

  • To perform queries on a separate thread.
  • To monitor the data source for changes and update the UI.
  • To integrate easily with the life cycle of Activity and Fragment.

Upvotes: 4

Andy
Andy

Reputation: 10840

The practical purpose is simply how Android handles UI elements (that is on the main thread). Basically, anything that may be a long running process, run it in a background thread so you don't lockup the main thread. This can't be said enough. After Gingerbread this has been more enforced by Android itself. Check out SQL helper. To get to the point in regards to opening an SQLite connection and its "speed":

Because they can be long-running, be sure that you call getWritableDatabase() or getReadableDatabase() in a background thread, such as with AsyncTask or IntentService.

By using CursorLoader, it makes your life easier if you need ContentResolver and are using SQLite DB. More importantly, it runs on the background. Just because you've never seen the DB lock up doesn't mean it doesn't happen. Better safe than sorry, and the main thread will thank you :)

Upvotes: 1

Related Questions