Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Using SquidCursorLoader

Sam Bosley edited this page Aug 24, 2016 · 12 revisions

SquidCursorLoader is the analogue to Android's CursorLoader. Instead of querying a ContentResolver to return a Cursor, it queries a SquidDatabase to return a SquidCursor. Queries done with SquidCursorLoader are asynchronous, so they won't block the UI thread waiting for the database.

To construct a SquidCursorLoader, you need an instance of SquidDatabase and a SquiDB Query object. Most often, you will construct a SquidCursorLoader in the context of implementing LoaderCallbacks:

LoaderCallbacks<SquidCursor<Person>> votersLoaderCallbacks =
        new LoaderCallbacks<SquidCursor<Person>>() {
    @Override
    public Loader<SquidCursor<Person>> onCreateLoader(int id, Bundle args) {
        Query query = Query.select(Person.PROPERTIES).where(Person.AGE.gt(18));
        SquidCursorLoader<Person> loader =
                new SquidCursorLoader<Person>(context, database, Person.class, query);
        return loader;
    }
    /* other methods */
};

Optionally, you can call setNotificationUri() to register a Uri to listen for changes on. If the Uri is notified of changes, the loader will be refreshed:

SquidCursorLoader<Person> loader =
        new SquidCursorLoader<Person>(context, database, Person.class, query);
loader.setNotificationUri(Person.CONTENT_URI);

When using the support library

If you are using the support libraries for fragments and require a version of SquidCursorLoader that also uses the support library, you can add the squidb-support-loader module as a dependency:

dependencies {
    compile 'com.yahoo.squidb:squidb-support-loader:3.1.2'
}

This module contains SquidSupportCursorLoader, a version of SquidCursorLoader suitable for use with the support libraries.

Note that squidb-support-loader includes a dependency on the Android support library (support-v4). If you want to force a particular version of the support library to be used in your own project, you should exclude this transitive dependency. For example:

dependencies {
    compile('com.yahoo.squidb:squidb-support-loader:3.1.2') {
        exclude module: 'support-v4' // Exclude the support lib dependency by module name
    }
}

There are various other gradle strategies to exclude transitive dependencies as documented here.


See also: