icon-image
Welcome to Innominds Blog.
Enjoy our insights and engage with us!

Realm For Ninja Mobile Developers

By Karthikeya Koppuravuri,

Realm DB for mobile appsThere has been a rush in the number of databases on the server side with several projects kicking off every quarter to rival old databases and to innovate new features. But the real issue is, how many of these databases can actually run on mobile, tablets and wearables, because they were never designed for this environment in the first place.

If you are a mobile developer like me, our only option is to stick with SQLite. We use it either directly or by using some wrappers on top of it, to make it convenient but it has been released more than a decade ago and our perception towards mobile and its applications has changed drastically over a decade. So, it is high time for the developers to look for an alternative and start afresh with a database which is compact and can handle mobile requests smoothly, in short a ‘mobile-first’ database.

What is Realm?

Realm is a database system. It’s like SQLite, except that there is no SQL in it. So, can we call it NoSql database? Yes, you are on track Ninjas! It’s a NOSql database. You define plain classes, these classes define your schema and Realm stores instances of these classes as objects. These realm managed objects are equivalent to your SQLite tables. You can now store query these objects as we do in SQLite tables.

Why Realm?

  • Simple: It is amazingly easy to use. Data is directly exposed as objects and queryable by code. It removes the need for ORM features that effect performance and maintainability.
  • Fast: It is incredibly fast and can retrieve close to 30 queries per second.
  • Offline mode: Offline access is one important feature for developers and is a blink of an eye far with Realm. Replacing your custom objects with Realm objects lets you store the data offline and it can be used without any hurdles. However, it needs a bit more work to write data offline but again, it is a painless way to let users see the data and interact with it in offline mode.
  • Build apps faster: Dealing with SQLite sometimes can prove a bit difficult. It demands a significant amount of your time and resources. But with Realm, it’s easy as your POJO classes from your schema.

Getting Started

Let us start with Android in the post. We will cover iOS later in another detailed post.

Prerequisites: Android Studio version 1.5.1 or higher, JDK version 7.0 or higher. A recent version of the Android SDK Android API Level 9 or higher (Android 2.3 and above)

Installation

Add classpath  io.realm:realm-gradle-plugin:3.5.0  in project level  build.gradle  file apply plugin:  realm-android  on the top of app-level gradle file. 

Initialising Realm

Before using Realm object it should be initialized first, use  Realm.init(context)  in your application file.

Ninja Mobile developers

Model Classes

Extend realm object to write the model classes.

r Ninja Mobile developers 2

Field types Realm supports multiple field types like boolean, byte, short, int, long, float, double, string, date, byte[]. In addition to these subclasses to RealmObject and RealmList are also supported.

Required Fields and Null Value

In some instances, null is not an appropriate value of an field. We can use the @Required annotation to tell Realm to enforce checks to disallow null values. With @Required, the annotations are possible only for Boolean, Byte, Short, Integer, Long, Float, Double, String, byte[] and Date. When fields with other types have the Required annotation, compiling fails.

Fields with Primitive types and the RealmList type are needed completely. Fields with RealmObject type are always nullable.

Autoupdating Objects

Any instantiation of Realm object is auto-updating, modifying the objects that affect query will be automatically reflected in the objects. This makes it Realm real fast and efficient. A need to refetch the data before updating the UI is not necessary.

Fetch Faster

The annotation @Index will add a search index to the field. This will make inserts slower and the data larger but again, queries will be faster. So, it’s recommended to only add index when optimizing specific situations to read performance. We support indexing: String, byte, short, int, long, boolean and Date fields.

Primary Keys

Use annotation @primary to make it behave like the primary key. It can be applied to field types like byte, short, int and long.

Conclusion:

Hopefully, this gives the basics of Realm. we can further talk on the types of queries, transaction blocks, object relationships etc., in our upcoming posts.

References:

Topics: Mobility

Karthikeya Koppuravuri

Karthikeya Koppuravuri

Senior Engineer - Software Engineering

Subscribe to Email Updates

Authors

Show More

Recent Posts