Realm is the newest kid on the block regarding mobile databases, as opposed to the older SQLite on Android. The key thing about Realm is that it is not an ORM (Object Relational Mapping) built on top of SQLite, but rather it's own persistence engine. This blog is not a study of how to integrate Realm, but more an exploration of why I chose to use it in the DebugRank app. Realm already has awesome documentation describing how to integrate Realm which can be found at realm.io. RealmSquare

Why I chose Realm for the DebugRank app

Controlling the Realm version

Using Realm allows you to decide what version of Realm to use in your app. Realm is installed with your application, whereas SQLite exists on the end user's device, and the SQLite version is only updated when the Android OS is updated.

Easy Fluent API

The query API is an easy to use fluent API, where most of my queries are 1 liners. This was a big plus for me to keep my code readable and maintainable.

Schema changes during development hack

When developing my pre-published app the schema might change multiple times. Usually during schema changes I'd have to manually clear the app data. Realm gives us a little hack for this, which obviously should only be used for pre-published development purposes.

This deletes my entire Realm DB whenever Realm detects a schema change and expects a schema migration.

Best practices

Repository Pattern

Although Realm comes with some nice utilities / add-ons to help directly populate RecyclerView Adapters, MapViews, and search results, you should always decouple your data layer from your UI layer using the Repository pattern. I won't dive deep into the Repository pattern itself, but conceptually it allows you to easily change your data layer implementation for development, testing, and production.

Ramblings

Bypass extending RealmObject

Ultimately my objects existed in a Java library, and, with Realm being Android only, I had to implement a strange hack. I created an interface describing my object, and then created a Realm wrapper in my android project that would extend RealmObject. I was hoping with the release of Realm 1.0 this would no longer be necessary by utilizing the RealmModel interface. Regrettably, support for inheritance and polymorphism is still in the works https://github.com/realm/realm-java/issues/761. Given that limitation, my solution is below:

Database browser

Realm comes with a utility app to browse your database (Mac only). Honestly this was always an uphill battle for me to get working, for Android or iOS Swift. Hopefully we can add some formal documentation on this topic the future.

No official Robolectric support

Robolectric lets me mock the Android SDK so I can run unit tests under com.company.app (test) instead of com.company.app (androidTest). This is important unit tests under com.company.app (androidTest) need to execute on an actual Android device or Emulator, which slows the execution down. Unfortunately to test my Realm reads and writes we still need to place these unit tests under com.company.app (androidTest).