Reputation: 1029
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
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
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
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 Loader
s 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 Loader
s. 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 Loader
s (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 Loader
s 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
:
Activity
and Fragment
.Upvotes: 4
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