Search code examples
androidandroid-loadermanagerandroid-databaseandroid-loader

Can the overhead of implementing ContentProvider for an internally used Database be avoided?


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!


Solution

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