Realm: an incredible fast mobile DB

NOV 01, 2015

Realm

Realm is a database designed for mobile platforms (Android and iOS). If you want to store just some user data and configurations, skip it. Instead, use simpler solutions like Shared Preferences (Android) or NSUserDefaults (iOS).

However, if your app needs to constantly retrieve data from the internet, you can help the user to reduce its mobile data usage costs by caching some info. Also, with cache, the user can use your app even when he's not connected. That's where Realm comes in.

Realm is not an ORM framework, but a database that have ORM support. If you need performance, forget SQLite or Code Data. Realm uses its own persistence engine to be blazing fast.

twitter benchmark

What it supports

Like any relational database, it supports primary and foreign keys, indexes, transactions and all CRUD operations. As any ORM framework, lazy queries, ignored properties and migrations are supported. Also, graph queries and encryption are nice features available.

For a full list, you can take a look into the docs summary for Java, Swift or Objective-C.

Limitations

Realm is great. Really great. But this section highlights what are Realm's drawbacks and what features they are currently lacking.

Support to NULL values

Ok, now Realm have support for NULLs! It was released for iOS and is currently in beta for Android.

A property that accepts NULLs is something very common and it was a bit sad to see that Realm didn't support it for a long time. Common solutions were to treat the default values as the same as a NULL or to add an additional boolean to point if its a NULL or not.

Distinct

Realm doesn't have a "group by" operator, but it have "aggregation" operators like min, max, count, sum and avg. However, as a group by is not available, you can't use distinct yet. You need to do it manually.

Nested Transactions

Nested transactions are a "must-have" in some cases to ensure consistency. As Realm lacks support for this, you may end up writing a little more code and maybe detaching objects before being ready to commit.

Migrations

I can't say that Realm doesn't have migrations. It has. However, I believe that its the hardest feature to learn and to use. As it is essential for the app life cycle, you may try this with a POC before adding Realm to your project. Some may even give up after trying to understand the documentation.

Getters and Setters (Android)

After designing your model, you have to write the Getters and Setters for each property. The bad part is that you can't add any other method and if you add validations or checks to your Getters/Setters, you will lose them since Realm replaces them with proxies that will ignore what you've done.

To workaround this, you may end up writing wrappers for each model class to be able to add business rules to ensure your constraints.

Overall

Realm is a great mobile database. As it has not achieved a 1.0 version yet and there are many features under development, stay tuned to the official website for updates!