In this tutorial, we’ll be discussing the basics of Realm Database and implement it in our Android Application for offline data storage.

Android Realm Database

Realm is a database that is the perfect alternative to SQLite. It’s is a NoSQL database designed for mobile platform.
Its core consists of a self-contained C++ library. It supports Android and iOS both.

Pros of Realm

  • Simplicity – Unlike SQLite the code is much shorter and concise. Besides, instead of using SQL, you just need to deal with objects and object trees in Realm.
  • Speed – Despite having a complex algorithm underneath, Realm performs CRUD operations faster. Obtaining objects is very fast since there is no deserialization
  • Live Objects – Realm has a no copy behaviour. All fetches are lazy, and the data is never copied until you perform some operations on them.
    So till then from a query, all that you get is pointers to data. Hence all objects received from Realm are proxy to the database. Due to this, zero copy is achieved. Whenever you need to access the data you will always get the latest value.
  • Great Documentation – Realm documentation is clear and the community support is great.
  • JSON Support Built-in – Realm has JSON support. You can directly set data from JSON without the need to create model classes.
  • Security – You can secure the database using encryption.
  • Reactive Programming – You can observe changes in the database and update the UI accordingly. Using RxJava along with Realm just makes it a lot more simple and fun.
  • Built-in Adapters – Realm has Adapter classes for the Android UI components.

Cons of Realm

  • No Auto-increment – You cannot auto-increment values in Realm.
  • Restrictions on Model classes – Besides the getter setter methods you cannot override methods like hashcode and equals in the Realm Model classes.
  • Threading – Real model classes can’t be passed from one thread to another. Hence you’ll have to query the class again on the different thread.

Configuring Realm in Android

Add the following classpath in your root build.gradle file:

Add the following plugin to the app’s build.gradle

Creating a Realm Instance

To create a Realm instance we do the following:

We can also pass a RealmConfiguration instance inside the getInstance()

Creating Realm Model class

To create Model classes, extend them with RealmObject.
Primary keys can be of String or integer type and are annotated with @PrimaryKey annotation.

Following are some annotations commonly used on Realm Model classes:

  • @PrimaryKey
  • @Required – Cannot be null
  • @Index – Add this on fields you are using in Query. It makes the queries 4X faster.
  • @Ignore

Writing Data to Realm

You can write data to Realm in the following ways:

There is a problem with the above approach though. It doesn’t cancel the transaction if failed.
Hence it is not the recommended way to insert data into the Database.

The method executeTranscation or executeTranscationAsync automatically handle the cancel transaction.

Also, we can add a try-catch inside the executeTranscation to catch exceptions. Try-catch is ignored by the earlier approach though.

insertOrUpdate or copyToRealmOrUpdate are used to insert if the primary key doesn’t exist or update the current object if the primary key exists.

Read From Realm

Again it’s a good practice to use this inside the execute method.

Delete From Realm

Following are the other methods that can be used to delete the results:

android-realm-delete-functions

RealmList is a built-in collection of RealmObjects and is used for model one-to-many relationships.

RealmList cannot support non-realm objects(such as Long, String, Integer).

In the following section, we’ll be creating a basic Android Application which uses Realm and performs CRUD operations.

Project Structure

android-realm-delete-functions

Code

The code for the activity_main.xml layout is given below:

Inside the MyApplication.java is where we initialise our Realm.

The code for the Employee.java class is given below:

The code for the Skill.java class is given below:

The code for the MainActivity.java is given below:

Don’t forget to close the Realm instance!

When you click update with a different Skill that doesn’t exist, it creates that and adds it to the RealmList.

The output of the above application in action is given below:

That brings an end to this tutorial. You can download the project from the link below:

By admin

Leave a Reply