3

Share

APP DEVELOPMENT

How To Use Realm Database In Android

  • Home
  • Blogs
  • How To Use Realm Database In Android

Realm Database is an open-source and object-oriented database. It is a lightweight, developer-friendly alternative to mobile databases like SQLite and CoreData.

Somish Kakadiya

July 18, 2022

Realm is a lightweight No-SQL database for Android, built with the CPP programming language. It occupies very little memory space compared to SQLite. So ultimately, it reduces the overall computation of our application. It is very fast as well, in reading and writing data as compared to SQLite.

Moreover, Realm is a cross-platform database that enables you to share your data across both the Android and iOS platforms. And it has a database browser called Realm Browser, which facilitates browsing your database easily. This is not based on a row-column structure, but it is based on an object tree.

It seems exciting, right? So, without wasting our time, explore the realm database thoroughly now.

Configuring Realm

Now to begin with firstly to configure realm modify your gradle build files as shown below:

1. Inside Your App Gradle Simply Use Apply Realm Plugin Below Default Android Plugin

2. Go To Your Project Gradle File And Enter Classpath Of Realm In Your Dependencies

3.Now Sync Your Project And Realm Will Be Configured For Your Project.

Adding Realm Model Class

To create model class, click on file >> new >> Java Class. Give appropriate name to this java file. I named it as Database.java. Model class must extend RealmObject/RealmModel class to use behaviour of Realm Database

Database.java:

Here as seen in image all realm Model classes should be annotated with @RealmClass or it will throw runtime exception. Moreover if your want any field to be primary key than annotate it with @Primarykey.

Now let’s explore Database operations step by step-

  • Create a new empty activity
  • Now Inside your oncreate of activity you need to initialize realm with:
Realm.init(this);
Custom Configuration

Attributing onto that you can also set a custom configuration to realm with this code:

All of this initialization is to be done only once per application.

3 - Now to start writing and reading data from database follow these steps:

1. Take a realm object in your activity eg:-

static Realm realm = null;

2. Now you need to initialize that realm object with this:

realm = Realm.getDefaultInstance();
Insert Operation

Now to Perform a normal insert operation into database look at this code:

Realm Async Transaction

Now if you want to listen for success and failures on database operations you can do that with realm async transaction.

Read Data

To Read Data from database take a look at below code:

Update Data

Now to Update data into database follow this example:

Fetch Data

Lastly, if you want to fetch all entries of a realm database into a single arraylist structure you can do this-

Edit, Delete, Or Drop

Here RealmResults is just a simple model arraylist from which you can fetch data just like a simple arraylist but it has one limitation at runtime if you want to delete or drop its data at specific position you are not allowed to do that so to solve that you can copy that realm results list into another list and then edit that other list.

Activity Code

Conclusion:

A Realm Database is easy to integrate into a mobile application. It is a good option for maintaining tables, writing queries, and performing several database operations. The best data storage solution for mobile and web applications as well. Hence, it will be effective to use in your new development project.

img

Somish Kakadiya

CTO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Latest 13 Web Development Trends To Expect In 2022
April 11, 2022 Category : company news

Revealing Vasundhara’s New Identity

Read More
Leave a Comment