Android Local Database Tricks with Kotlin and ObjectBox.

For those of you looking for a simple way to store large chunks of JSON data into a local database in Android, then look no further. There are so many times when we come across offline requirements of accessing big chunks of data from a REST online database and saving it locally for offline use later but do not want to go through with writing excessive and large amounts of boilerplate code to do it and we would like a way to do this simply and most effectively.

Here I present a really simple and easy way to save large data into your local DB without relying on Shared Preferences or the need to do lengthy ORM mapping or creating custom converters for your data classes. Shared Preferences was one option but it is normally only used for saving small pockets of data like settings — the other downside is that if large data is saved into Shared Preferences it takes a really long time to access the Shared Preferences data again.

So welcome to ObjectBox. What is ObjectBox?

is a really cool super fast object-oriented mobile database that persists objects, built uniquely for IoT and Mobile devices. It lets you avoid many repetitive tasks and offers a simple interface to your data. It seems to have been adopted by the Android engineering world really well so far replacing SQLite databases quite rapidly.

Below is a quick and simple solution for developers looking to rapidly setup ObjectBox and also have a way to store data quickly without having to write custom converters for each and every data object in their DB.

So let’s begin — Firstly install ObjectBox.

Add it to your project in your root build.gradle.

In your app build.gradle module add this.

In your dependencies add this:

Once thats done, we’re going to want to initialize ObjectBox and create a BoxStore reference.

In your Application class write something like this.

Then in your models create an ObjectBox singleton like so.

Now you have initialized ObjectBox in your project and will have access to the BoxStore ObjectBox in your project.

Okay now for dealing with your JSON data.

If you do not know how to convert your JSON into data models quickly then please read my article . Basically convert your JSON data into Kotlin data models using Pojo Generator.

Once all that is done and your API calls and services are setup and done, create another data class and call it DataObjectBox like so.

You can have as many “data” String properties as you like in this class and name them whatever you feel but only one is needed if you have one big payload dump. The data variable will hold all the data as one long String for the JSON payload.

Now the fun part comes in.

In your Activity.

As you can see we retrieve the JSON dump, then with the assistance of Gson we deserialize it from its original object into a string and then dump it into the payloadBox data property and viola! We now have access to that data at anytime in our app.

To access our data again we simply do this.

Now whenever you need to play around with the data you have it!(payloadDataOriginalObject) — ResponseMainDataDTO would be your top level data class.

This method alleviates any necessity to write custom converters and Entities for all your data classes — removing the whole ORM mapping process.

To write:

Cheerio and happy coding!

You can also follow me on

Life long seeker

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store