Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Why Realm, not SQLite?

Vicky Chijwani edited this page Nov 19, 2017 · 1 revision

This is an honest evaluation of Realm against ORM and ORM-like solutions (DBFlow, SQLBrite, SnappyDB, ORMlite, GreenDAO, ...).

Realm

Pros:

  • 100x less cumbersome than raw SQLiteOpenHelper + ContentProvider + Loaders.

  • Screaming fast; large queries on the UI thread do not have significant perf impact. (To quote the developer of Falcon Pro 3: "I queried ten thousand Tweets on the main thread, and it performed so well. So, I went with Realm." But it's important to note, he also said in the same breath that Realm queries are less flexible than normal SQL, which is of course true).

  • RealmChangeListener or an EventBus are simple, easy-to-use substitutes for CursorLoader.

Cons:

  • One general conceptual tripwire is the fact that queried RealmObjects are attached to the DB at all times (i.e., updates to them will be reflected everywhere unless they're copied). Programming with this concept in mind can get tricky at times, because we're conventionally used to thinking of data objects as being merely in-memory representations of persisted data.

  • RealmObjects cannot be read or modified, even in memory, once the Realm is closed or the object is removed from its Realm. This is the cause of many unexpected crashes during development (although they are obvious in hindsight due to a clear error message). However an official workaround for this is being considered. RealmObjects can now be deep-copied!. Unfortunately you still have to remember to make the copy before handing objects to the UI, which is very easy to forget.

  • RealmObjects cannot be passed across threads, so all db queries must occur on the UI thread! To be fair, Realm is already screaming fast compared to SQLite, but this is not ideal for very large queries (although a counter-argument could be made: why are you querying so much data simultaneously when a user cannot possibly make use of all of it simultaneously?). Realm now supports async queries. Go Realm team!

  • RealmObject subclasses cannot have custom instance methods (not even equals() and toString()!). This restriction no longer applies.

  • The above restriction means Realm does not allow implementing the immensely-useful Parcelable interface on RealmObjects. There is a workaround, using Parceler, but that is a huge library with > 28,000 methods, so I wouldn't recommend it. The alternative is to manually do what Parceler does (namely, make a separate class that wraps the RealmObject and implements `Parcelable). This restriction no longer applies.

  • Must extend RealmObject. This restriction no longer applies.

Clone this wiki locally