My Hexaville
Spead the Word

Android Realm Instead of SQLite

android realm
Designed by Freepik

Hi, if you ever needed to store complex data, more complext than just primitives and store a lot of it, then most likely used SQLite. Plain Android API has a huge overhead: declaring contracts, creating databases, using SQL. You might used some libraries like Sugar ORM which eliminate that huge overhead and need to write SQL, but today we’re going to look at the database that is way more perfomant than any other

Why Database?

First of all, why should you use database for your app in the first place? If you display bunch of data from server, then it’s a perfect use for storing it in a database.

Without one you would fetch data from server each time you restart activity. Which makes no sense because most likely data doesn’t update that often. Even if it is, most of it isn’t going to be a new one. If you don’t like progress bars appearing all the time, wasting extra bandwidth or pinging server in vain, then caching data with a database is for you.


The default SQLite solution has too much boiler plate code, but you have lot’s of control and see what’s that you’re building and the result. There’s some libraries to eliminate SQL use and boiler plate code


Realm is the fastest database, you can check some stats here and it’s simple to use. So how do we setup?

Add this line to your project’s or module’s build.gradle

As you see it’s a Gradle plugin, if you want to read more about those, check this post. And now add this line to your module’s build.gradle

Create your custom application and add this line.

Create POJO

First thing we need is create a POJO. I’ll use Kotlin here to show how much better to use it instead of Java, it has quite a few great points about it. For your model you just need to extend RealmObject, have a default constructor without arguments and be non-final. With Kotlin 1.1 you can do it with one line.

Open keyword to make it non-final and we declare our constructor right there. But as you see we make our variables nullable, because if you make all of them nullable, it will create a default constructor for you.

This way you wouldn’t get one.

Save Data

Then get reference to Realm in your activity and don’t forget to close it on destroy

And save data, you need to save it in translations always

Read Data

Realm has amazing API, but now let’s just get the first found data.

To check the whole database you can use this app on Mac and for Windows, Linux you can use Realm Stetho. Add those lines to your module’s build.gradle

And those to application

Then when app is launched open Google Chrome, type chrome://inspect in address and click inspect

android realm

And open this tab to see your whole database

android realm

You can check shared preferences and more using Stetho


As soon as you change some of your models you’ll get exceptions, I’m not going to go through details of migrations, but during development it’s better to just delete previous database on any changes. Replace your Realm.getDefaultInstance with this

You can check more info about migrations here


Alright, the basic API is pretty simple to use, you can get source code here, check this video for mode details. Official documentation here, don’t forget to subscribe, follow me on Twitter, Facebook, G+ and share with friends if you think this will benefit them!




About the Author Ihor Klimov

Formerly an Android developer, lately picked up some Flutter. This blog is everything that I find exciting about Android and Flutter development. Stay tuned and hope to see you again!

follow me on: