Reputation: 8578
The Android developer guide states :
You don't need to develop your own provider if you don't intend to share your data with other applications.
So It is understood that if i have a SQLite DB used entirely locally in the app then a ContentProvider should not be needed (having a lot of boilerplate code writing avoided), but I didnt get whats the "Android" way to query this DB on the background? (with as less overhead of re-inventing the wheel as possible)
It may seem that this task is what Loaders are for, as sateted in the docs :
loaders make it easy to asynchronously load data in an activity or fragment.
but there is no subclass of Loader
that can carry this task out, The closest subclass - CursorLoader is desgined to be coupled with a ContentResolver
. So, am i missing something? Is there a third party implementation of Loader that does that or is there another approach?
Thanks!
Upvotes: 0
Views: 159
Reputation: 1489
No need for ContentProvider if you don't
share data with other processes (applications);
build App Widget;
integrate your own search suggestions with the Android Quick Search Box;
... and you do not want a fancy syncing and querying data with auto-UI update.
Actually, you can try to wrap read/write DB operation in AsyncTaskLoader
(AsyncTask that respects activity lifecycle). Also you will have to implement your own observers to update views (one more option to use data binding).
Upvotes: 0
Reputation: 1007349
whats the "Android" way to query this DB on the background?
Whatever you want. Use an AsyncTask
. Use a plain Thread
. Use an IntentService
. See if your preferred ORM has an asynchronous option, or hooks into RxJava/RxAndroid, or something.
IOW, there is no single "Android" way.
It may seem that this task is what Loaders are for, as sateted in the docs :
A Loader
is only relevant in cases where you want the UI layer to be working with the database fairly directly. You can only use a Loader
with an Activity
or Fragment
.
am i missing something?
You are welcome to create your own subclass of AsyncTaskLoader
that works with SQLite directly, perhaps using the source code to CursorLoader
as a source of inspiration.
Is there a third party implementation of Loader that does that
I had one, once. However, I discontinued it.
Upvotes: 2
Reputation: 36045
A Loader
is a mechanism that hooks in to the Activity lifecycle that can be used to asynchronously load any kind of data whether ContentProvider, network, or you just need to crunch a bunch of numbers.
At the base there is a Loader called the AsyncTaskLoader which a CursorLoader
actually inherits from. This can be used to provide the base level functionality of a Loader. All you have to do is implement a
data model that you need to load, then implement the AsyncTaskLoader#loadInBackground()
method to get it. In this case, you would query the database then return the results in a form of your choosing. Maybe in this case you don't want to return a Cursor
but you want to create Pojo's that represent a row in your Cursor for example.
As a quick implementation, you may be able to take CursorLoader's source code and have it query from your database instead of using a ContentResolver
.
Upvotes: 1