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.
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.
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-
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:
Now to Perform a normal insert operation into database look at this code:
Now if you want to listen for success and failures on database operations you can do that with realm async transaction.
To Read Data from database take a look at below code:
Now to Update data into database follow this example:
Lastly, if you want to fetch all entries of a realm database into a single arraylist structure you can do this-
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.
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.
CTO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!