Search code examples
androidsqliteloaderandroid-cursorloader

AOSP contribuition: CursorLoader for Sqlite DB instead of ContentProvider


Why is there no SqliteCursorLoader in android platform? This would be similar to CursorLoader but for a SQLite DB instead of a Content Provider. I found some implementations around the web, and it seems to be useful.


Solution

  • Why is there no SqliteCursorLoader in android platform?

    Because it would be very difficult for one to fulfill the Loader contract. There are three things that a Loader needs to be able to do:

    1. Do the work asynchronously. This could be handled by inheriting from AsyncTaskLoader, so there is no real problem here.

    2. Retain the loaded data across a configuration change. This is provided by the framework, so there is no real problem here.

    3. Automatically deliver updated content when the content changes.

    Item #3 is where the problem lies. The only reason why this works with CursorLoader is because the ContentProvider system already has the notion of ContentObserver and already expects providers to make the appropriate notify calls to let the system know about data changes.

    So, it is not merely having to implement a SQLiteCursorLoader, but also having to ensure that everywhere that you modify the database, you raise an event (e.g., via LocalBroadcastManager) that the Loader could listen for in order to know it needs to requery the database and deliver a fresh Cursor.

    Now, for internal purposes on a project, you might be willing to skimp on the contract and skip item #3. That's perfectly cool. However, I would not recommend distributing such an implementation (e.g., as an open source project), due to that limitation. And this is why I discontinued my SQLiteCursorLoader (beyond a general belief that the Loader framework is a failed abstraction).