Developer Aspirations

YAPB - Yet Another Programming Blog

Thursday

14

August 2014

SimpleNoSQL For Android Released

by Colin Miller

I'm announcing the release of a new local data framework for Android that I've been working on called SimpleNoSQL. It's an easy way to store and retrieve objects to/from disk without dealing with files or SQL. You can check it out here on github.

For many projects, we merely want to save data and retrieve it again. This is where SimpleNoSQL comes in.

Say I have some class that contains my data:

public class MyData {
    public String name;
    public String title;
    public int goRating; //for how good at Go you are
}

And I want to save it to disk. This is how I can do it with SimpleNoSQL:

MyData data = new MyData();
data.name = "Colin";
data.title = "Developer";
data.goRating = 9;
NoSQLEntity<MyData> entity = new NoSQLEntity("personBucket", "colin");
entity.setData(data);
NoSQL.with(context, MyData.class)
    .save(entity);

That's pretty much it. I didn't have to do any setup of databases. I didn't have to set up an AsyncTask, Service, or some sort of executor. The operation happens on a separate thread and saves that entry into a bucket called "personBucket" with an id of "colin". The second id argument is optional; the system will generate a random UUID if you don't supply an explicit one.

Let's see how we retrieve that entry:

NoSQL.with(context, MyData.class)
    .bucket("personBucket")
    .entityId("colin")
    .retrieve(new RetrievalCallback<MyData>() {
        public void retrievedResults(List<NoSQLEntity<MyData>> entities) {
            MyData data = entities.get(0).getData();
            // display it or something
        }
    });

Since RetrievalCallback is an interface, you can either declare it inline like I did above, or have your activity or fragment implement it. The request is asynchronous and will query the data on a background thread. When the result is ready, your RetrievalCallback method is called on the UI thread so you can display your results.

That's great for a single entry, but we can also grab an entire bucket. This is really useful for things like a list of items a user is entering and you want to retrieve the entire list:

NoSQL.with(context, MyData.class)
    .bucket("personBucket")
    .retrieve(new RetrievalCallback<MyData>() {
        public void retrievedResults(List<NoSQLEntity<MyData>> entities) {
            for (NoSQLEntity entity : entities) {
                MyData data = entity.getData();
                // do something with this
            }
        }
    });

Additional features include filtering of the data based on a supplied filter. You can also order the results of a list using a Comparator. Specific entities or entire buckets can be deleted. Saving a specific entity will either create a new item if an entity in that bucket with that ID does not already exist, or replace the existing entry so you don't need to worry about inserting vs updating.

You can include SimpleNoSQL into your project by adding the following to your project's build.gradle file:

repositories {
mavenCentral()
maven {
        url 'https://oss.sonatype.org/content/groups/public'
    }
}

dependencies {
    compile 'com.colintmiller:simplenosql:0.5'
}

I created this framework to support some other projects I've been working on. I had an app where the user would enter information into it, and I wanted that information to persist between sessions. Often these things were lists of things, but sometimes it was just configuration. I wasn't pleased with the standard way of doing this on Android.

By default, android comes with support for sqlite3 as a database layer. This is fine if you want to create a relational database, but I found this was often too bulky for what I wanted to do. To save even a basic object into a database, you have to create a class that extends SQLiteOpenHelper and write a lot of Broiler-Plate for setting up your database, your tables, what each of the columns will be, your creation step, your upgrade steps... etc. Then for each type of query you have to write SQL queries including which columns you want back, how to turn the DB data into objects, setting up any parameters you might want to have in where statements, and a few other odds and ends. After you've done that, you normally need to set up either a Loader or some sort of AsyncTask to retrieve the data and return it to the UI thread for display. That's a lot of work to do for every app. In addition, if you decide to change your data model, you need to change all of this SQL code.

The project is currently in active development with some new features in the works. I mainly made this for myself for my pet projects to have a quick and easy data layer. Hopefully you may find it useful as well. I'm very open to both feedback and requests. Here's the project URL again:

https://github.com/Jearil/SimpleNoSQL

comments powered by Disqus