Introduction to Realm: Why you don’t SQLite anymore?

What is Realm?

Realm is a third party database tool that claims to be a replacement for SQLite and CoreData and attempts to simplify a issues encountered while writing SQLite such as boilerplate code, threading support and asynchronous queries. Currently, you can use Realm in your Android app built using Java, IOS app built using Swift or Objective-C or a cross platform app built using React Native.

realm

How to setup Realm in Android?

1. Realm does not work with Java outside of Android at the time of writing this post.
2. Use Realm with the latest version of Android Studio whose version >= 0.8.6 and Java JDK whose version is >= 7
3. To understand the usage of Realm, we consider a bucket list app called BucketDrops. Check the links section for more info on this app.

4. Create a new project in Android Studio, call it BucketDrops and open its build.gradle file corresponding to the module app.

5. FInally, add compile ‘io.realm:realm-android:0.87.5’ to the dependencies of your project and sync your gradle files.

Realm with Java
Models

A model represents the data that you want to store, which in our case is the goal that a person wishes to achieve (what), the time it was added (added), the target time or the time by which you would like to accomplish the goal (when) and whether the goal was marked complete by the user or not. Let’s examine the model with SQLite and Realm.

SQLite

What Added (Primary key) When Completed
Create the above table in SQLite with Added as the primary key.

Realm

You don’t need to create a table for the same while using Realm.

1. Create a class in Java.
2. Extend from RealmObject which essentially qualifies your class as being the model.
3. Add the columns or fields which you would prefer as instance variables inside the class.
4. Ensure those instance variables are private.
5. Create getter and setter methods.
6. Don’t add any custom logic inside these methods.
7. Use the @PrimaryKey annotation to mark a field as the primary key
8. Use the @Ignore annotation to mark a field so that it is not considered as a column in your table.

Refer to SNIPPET 1 from HERE http://dgit.in/RealmCode
That’s all you need to make a model in Realm. Each class you create which extends from RealmObject can be considered to be a separate table while visualizing the data.

Data Types

Realm supports the following field types: boolean, byte, short, ìnt, long, float, double, String, Date and byte[]. The integer types byte, short, int, and long are all mapped to the same type (long actually) within Realm. To represent relationships, Realm uses something called a RealmList which is conceptually similar to an ArrayList in Java. The RealmList can work with boxed data types such as Boolean, Byte, Short, Integer, Long, Float, Double and RealmObject itself allowing you to create a RealmList<? extends RealmObject>.

Annotations

Realm uses several annotations such as @PrimaryKey @Index @ Required @Ignore to let you define certain requirements in your data model.

@Required: Lets you mark a field so that it cannot have null values.

@Ignore: Lets you mark a field so that its value is not saved to the database. For example, you may want to use the same model class for representing JSON data with some additional fields and store only some of them in the database.

@Index: Adds a search index to the field which makes inserts slower and the data file larger but queries will be faster. It is recommended to add index when optimizing specific situations for read performance. Realm supports indexing: String, byte, short, int, long, boolean and Date fields.

@PrimaryKey: Specifying this over a field lets you mark that field to be used as a primary key by Realm. You can apply this to a field whose data type is either an integer (byte, short, int, long) or String. It is currently not possible to use a compound primary key. Primary keys cannot have a null value, and the @PrimaryKey annotation uses the @Required annotation implicitly.

Limitations

The following restrictions apply when you are creating a model class in Realm.

• Instance fields should be kept private.
• Don’t write custom logic inside getter and setter methods as they won’t be executed.
• Both public and private static fields are allowed along with static methods.
• You can implement interfaces without any methods in them.
• It is not possible to override methods such as toString() or equals().
• Limitations with respect to other aspects of Realm are explained in detail HERE (https://realm.io/docs/java/latest/#general)

Relationships

Let’s examine our Bucket Drops app where multiple users can use the app on the same device and add drops to their bucket list. We need to track which person added the drop and use relationships for that purpose. We create a class called Person with a relationship to the Drop class which we created earlier. Refer to SNIPPET 2 from HERE http://dgit.in/RealmCode One Person object can be related to many Drop objects with the help of the RealmList class. Similarly, one-to-one and many-tomany relationships can be modelled easily. It can be visualized as shown below.

drop

Now that you have seen how relationships look like in Realm, lets get to the fun part where we perform some operations on this database in the next section.

Leave a Comment

Your email address will not be published. Required fields are marked *