ObjectBox: Alternative to SQLite for android

How many times do we need to write CRUD operation while building android application? Many times right? For me its like daily job.

Till now I used to use sqlite database. But when I came across ObjectBox , I started liking it. Why? Because it saves me a lot of time in writing bucket of code which I used to spend while writing sqlite db query also it was a bit complicated / confusing while reading those queries. While in case of ObjectBox your queries becomes easy and readable.

ObjectBox is open source, NoSql and a super fast mobile database that persists objects. It  avoids many repetitive tasks and offers a simple interface of data. It is used to easily manage app’s data into local storage.

Now let’s look into Core Classes of ObjectBox

MyObjectBox: Generated based on your entity classes, MyObjectBox supplies a builder to set up a BoxStore for your app.

BoxStore: The entry point for using ObjectBox. BoxStore is your direct interface to the database and manages Boxes.

Box: A box persists and queries for entities. For each entity, there is a Box.

Let’s get deeper and see how to integrate it and develop a simple application.

Note: Here I am demonstrating CRUD application using kotlin but its very simple and similar to integrate it with Java.

Add ObjectBox Dependencies

In the project level build.gradle file add the following

buildscript {
    ext.kotlin_version = '1.3.50'
    ext.objectboxVersion = '2.4.0'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
    }
}

Open the build.gradle (Module :App) add the following

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.objectbox'

Now  create the Entity class Notification.kt that contains the properties id, title, and brief and fileSource.

import io.objectbox.annotation.Id
class Notification {
    @Id
    var id: Long = 0
    var title: String? = null
    var brief: String? = null
    var fileSource: String? = null
}

Lets initialize ObjectBoxApp class by extending android Application class and hold the BoxStore.kt reference which will be needed to execute different data CRUD operations. ObjectBoxApp.kt class will look like this :

class ObjectBoxApp : Application(){
    private var boxStore: BoxStore? = null
    override fun onCreate() {
        super.onCreate()
        boxStore = MyObjectBox.builder().androidContext(this@ObjectBoxApp).build()
    }
    fun getBoxStore(): BoxStore? {
        return boxStore
    }
}

Lets set up BoxStore to create a notificationBox.

val boxStore = ((context as MainActivity).application as ObjectBoxApp).getBoxStore()
val notificationBox = boxStore!!.boxFor(Notification::class.java)

Here, Notification is an ObjectBox entity. And now that we have its Box, we can start insert and retrieving data from the database.

Insert data into notification box

var notification = Notification();
notification.title = "5 Key Mobile App Statistics App Developers"
notification.brief = "James Ewen looks at five interesting stats based on data from the last year. Then he's going to attempt to understand what these trends show, how it will affect monetization, engagement and other app metrics."
notificationBox.put(notification)

Get all record

val notificationList = notificationBox.all

For delete notification

val notification = getPlayerById(id)
if (notification != null) {
    notificationBox.remove(id)
}

 

Conclusion:
A simpler way to handle data. It reduces lines of code and avoids query complexity. It is up to 10 times faster than SQLite.

Happy coding. Feedback is most welcome 🙂

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s