Part II – Writing Better, Faster and Easy to Maintain Mobile Apps with Realm

Part II - Writing Better, Faster and Easy to Maintain Mobile Apps with Realm

SHARE

WRITTEN BY

Rohan Pawar

24 November 2017

Stay updated with our Newsletter

In my previous blog post, I described Realm’s seamless synchronization between devices and applications. In this part, we are going to discuss implementation and usage of Realm.

You do not have to create a table, helper etc. in Realm. Just create POJO class, which will be a subclass of RealmObject and you are ready to start using Realm. Here is how to use Realm.

Define Model

Code editor showing Profile public class

Code editor showing InfoDevice public class

Realm accepts all primitive types and its boxed types (except for char), String, Date and byte[]. It also supports subclasses of RealmObject and RealmList<? extends RealmObject> to model relationships.

Initialize Realm

Code editor showing Realm Initialization

Supports synchronous and asynchronous

Synchronous

Code editor showing synchronous transaction

Asnchronous

Code editor showing asynchronous transaction

Relationships

Any two RealmObjects can be linked together.

Code editor showing linking between two Realm objects

Queries

All fetches (including queries) are lazy in Realm, and the data is never copied.
Realm’s query engine uses a Fluent interface to construct multi-clause queries.

Find one

Code editor showing Find query

Find all

Code editor showing Find all query

Filtering

For all data types, you have the following predicates:

  •     equalTo()
  •     notEqualTo()

Numeric data types, including Date, allow these additional predicates:

  •     between() (Both end points are included, i.e., it is a bounded interval)
  •     greaterThan()
  •     lessThan()
  •     greaterThanOrEqualTo()
  •     lessThanOrEqualTo()

String fields allow these additional predicates:

  •     contains()
  •     beginsWith()
  •     endsWith()
  •     like()

and one or more wildcards:

  •     * matches 0 or more Unicode characters
  •     ? matches a single Unicode character

predicates to check for emptiness:

  •     isEmpty()
  •     isNotEmpty()

Two useful predicates for matching the null value are:

  •     isNull()
  •     isNotNull()

Supports Other Libraries

GSON, Jackson-databind, Kotlin, Parceler, Retrofit, Robolectric, RxJava

Speed

Graph showing Realm's query speed against others

This was a quick primer on how to make your mobile apps better, faster and maintainable using Realm.

  • Android
  • iOS
  • Mobile Apps
  • Mobile Database
  • Realm

SHARE