Android TutorialsLearn about REALM: a new Library for Database Model in Android

Learn about REALM: a new Library for Database Model in Android


What is Realm?
Realm is basically a free object orientated database along with open source libraries for iOS as well as Android. The concept follows the ACID principle by using a powerful idea that focuses on ease of use. The internal storage engine known as Tight DB, which is basically a closed resource now, but it will be open sourced quickly. REALM is a new local mobile database framework. It has many advantages over the old ones, which will be discussing later.

Overview of Realm:
Android development has traditionally been a complex task. What was once complicated and time consuming is now slowly becoming simplified and light on devices. One of those recent trends in Android development is Realm. It is an organized database system specifically created for the Android operating systems (also available for iOS). Realm adopts a new, yet familiar approach to your database needs and helps you manage better and faster than SQLite. Since, the syntax is similar to SQL it saves time on having to learn any new language.

The objective of Realm is to simplify database management and provide an easier method of application, as well as being swift in its pace. It comes with many advanced features that are unavailable in the previous line of work. Realm not only provides a simplified method, but is able to replace both SQLite as well as the ORM libraries. Having its own persistence engine, Realm is able to provide a faster and more simplified application. This means you get faster running applications and better user experience. Developers are able to code and upload their programs within hours as compared to the former approach, saving a lot of time and hassle in the process of creating apps.

Realm also features many add-ons in addition to what is already available. The usage of this database has progressed so far that already there are plenty of third party add-ons present to facilitate you on your specific programming needs. What is remarkable is the cross platform file sharing capability. The models are very similar to the already existing Objective-C, Java and Swift. Thus, eliminating any need to spend much time learning any new methods in order to be competent in the application of Realm. This new trend in database has led to many prominent new apps in the market currently. Realm users include Starbucks, Groupon, Alibaba, BBC and many more.

Features in Realm:
For a repeated item, SQLite will make several copies. REALM acts differently, there will be one main copy, and all others will point to the same main copy. This is called Zero Copying. This way it consumes less memory.

REALM is implemented in C++ with a very small library size. It is column oriented and cross platform. You can easily see records with the help of the Realm Browser.

If you haven’t yet downloaded the Realm Browser, download it now from the Mac App Store as it is currently available for MAC OS and then  you  can open the default.realm file with Realm Browser easily.

Getting Started with Realm Configurations:
Classes: In REALM, we store data using Java classes. The concept is basically a class to be stored that is extended by “RealmObject”. Primary Key (PK) is used for uniquely identifying objects.
Annotations: @PrimaryKey annotation is used for setting a class variable as PK. There are many other annotations, @Ignore @Required, etc. @ignore is used to leave out a variable from the database. @Required makes a variable mandatory to fill.
Variable types: int, long, short, Boolean, float, double, Date, String and byte[] are supported.

Learn Android Development From Scratch

Relationships among classes
As SQL databases have 1-to-1, 1-to-n and n-to-n relationships among its tables, REALM also has the same kind of relationships among its classes.
One-to-One: Make the object of a class as other’s variable. For example, if a person has only one car, and we want to store the car’s info in person’s class. We will make an object of Car class as a variable of  the Person’s class.
One-to-Many: For this, we have to make a list of objects of class into another. For example, if a person has many cars, the person’s class will have RealmList<Car> instead of just one object. We can add as many cars as we want.
Many-to-many: REALM array property is used for the purpose. The procedure is to store objects in this array and define relationships among them.

Realm model in SWIFT:

Example Implementation

public class Car extends RealmObject {
 private int regNum;
 private String model;

public class Owner extends RealmObject {
 private int id;
 private String Name;
//One-to-Many Relationship
 private RealmList < Car > ;
 //Add as many car objects as you want Later

Next, we will study how to store objects.

A DB Wizard for the Android OS:

Some Limitations in Realm:
JSON Realm provides the capability to directly create JSON right into a realm. While de-serializing directly into the Realm is achievable, there is absolutely no reverse of a Realm Object to JSON. Additionally, it is not possible for you to de-serialize a JSON object straight into another realm object (effectively making a sub-object). For example, adding a green shirt through JSON instantly to the peter person is not feasible in REALM.

Unlike other server-side database, Realm is the first mobile database designed for phones and tablets.  They have a smorgasbord of features evidently better than the ones before: SQLite and MySQL.  It makes use of lesser resources which makes it fast, safe, easy to use, and even gets your app working in just a few minutes.

It can lessen if not remove entirely the months of attending to data bugs.   It allows easier and quick coding and is the best alternative for SQLite (Android) and Core Data iOS for Apple devices.  Realm makes use of less API and has a custom C++ core that allows your app to keep millions of records and also prevent it from crashing under such loads. The new Realm mobile database will make app creation faster and more possible for you!


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -Eduonix Blog