-
-
Notifications
You must be signed in to change notification settings - Fork 59
Why Realm, not SQLite?
This is an honest evaluation of Realm against ORM and ORM-like solutions (DBFlow, SQLBrite, SnappyDB, ORMlite, GreenDAO, ...).
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 anEventBus
are simple, easy-to-use substitutes forCursorLoader
.
Cons:
-
One general conceptual tripwire is the fact that queried
RealmObject
s 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. -
RealmObject
s 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.RealmObject
s 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. -
Realm now supports async queries. Go Realm team!RealmObject
s 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?). -
This restriction no longer applies.RealmObject
subclasses cannot have custom instance methods (not evenequals()
andtoString()
!). -
The above restriction means Realm does not allow implementing the immensely-usefulThis restriction no longer applies.Parcelable
interface onRealmObject
s. 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 theRealmObject
and implements `Parcelable). -
Must extendThis restriction no longer applies.RealmObject
.