75

I'm building an app and I need to use a database in it. I'm considering using Room as it's new and hot right now. But I've heard a lot of great stuff about Realm too. Can someone point out the possible advantages of using each?

I did my research and read the docs but I have no experience to understand it myself

Room docs

Realm docs

EDIT:

It's been some time and and I can point out another reason to use Room. Although you can use Realm and Transformations to tie things up with LiveData but with Room you can directly return LiveData (and also RXJava types using a plugin) for the DB, which will save you some boilerplate code and headache.

EDIT 2:

In addition to the first edit, Room now has a first class support for coroutines in Kotlin so if you are planning to use those it will be a nice-have -> https://medium.com/androiddevelopers/room-coroutines-422b786dc4c5

Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • Have a look at https://www.reddit.com/r/androiddev/comments/6dj652/realm_vs_room – AesSedai101 Nov 10 '17 at 09:20
  • @AesSedai101 thanks for the link , it' useful to read. The only con is the discussion took place 5 months ago, some opinions could change as more people started to use Room – Rainmaker Nov 10 '17 at 09:53
  • 1
    Regarding this question being closed - I don't agree that this question is opinioned based. This question is asking for architecture direction and key considerations when choosing between two approaches / libraries. Some answers may be opinion based, but if the question is answered well it will provide objective decision making criteria to developers. – socasanta Jan 16 '21 at 18:11

2 Answers2

88

Realm

A relatively fast and convenient library, all links are simply implemented, which is related to the object orientation of the database. Excellent documentation. Is, perhaps, one of the best option for storing data on a mobile device at the moment, the minus can only be an increase in the size of the apk-file by 2.5 MB.

Room

An interesting solution presented on Google I / O 2017 as optimal for working with the database on Android OS. Despite the fact that it is necessary to use explicit sql-requests, the library turned out to be quite convenient and I liked it personally. On performance is in the lead, so I would advise you to choose this particular library. Big advantage of this is based on build-in SQLite database. Since this solution, submitted by Google, it will quickly become popular, and, therefore, there will be no problems with finding solutions to problems that occur along with it.

Realm uses more RAM and increases the apk size, build time. So I prefer Room.

There are comparison: https://github.com/AlexeyZatsepin/Android-ORM-benchmark

Alexey Zatsepin
  • 1,179
  • 7
  • 14
  • 1
    I had never used it, but in my opinion it will have same problems and same benefits like Realm. – Alexey Zatsepin Mar 19 '18 at 14:24
  • 1
    @AlexeyZatsepin if you split the apk based on the architecture then it wont take 2.5 mb space. it will get reduced based on the architecture. – Ravi Yadav May 23 '18 at 12:31
  • I tried it in a demo app, it's not bad (easy to use), but the documentation is just not good enough. Also there seems to be a bunch of issues that they still need to resolve first (e.g. I have waited 2 months for their rxjava adapter thing to work properly). And one of the biggest issue I have is the auto migration. It sounds great on paper, but it also has the potential to make things unpredictable and untraceable, and the not exactly good documentation means I end up messing up the db once or twice. (cont.) – Louis Tsai Jun 11 '18 at 10:49
  • I ended up moving to room for the production version of the same app, to get a better balance between ease of use and stability (room is still new but at least sqlite is not and it's not too hard to find resources or people that can help) – Louis Tsai Jun 11 '18 at 10:49
  • How about GreenDao? What are your thoughts over "GreenDao vs Room"? – Daksh Gargas Jun 22 '18 at 06:24
  • 2
    for realm, your benchmark comparison is unfair. It is based on a very old version of realm, but more importantly you use inefficient code for the implementation, making it appear slower than it is if used properly – Tim Nov 20 '18 at 10:51
  • @Tim also unfair is that in other ORM's caching was disabled, but for Realm it was enabled, making it appear faster. – Erik Aronesty Jul 23 '20 at 13:05
11

The main reason that we pushed to use a library for database is the fact that it let us model our objects and made CRUD easier, I had a good experience with Realm, it's really easy to set up and work, it's fast and flexible, but size of the library was an issue, it's possible to reduce APK size by splitting APKs on build target ABI but I preferred to use GreenDao because it's based on SQLite, although I think it has some disadvantages like any other libraries but it was the best option for me.

Honestly, I didn't try Room yet but with a brief look at the documents you will find it more flexible and friendly to developers, As a Google fan, I prefer to use Google guys' library! As I said before I prefer a layer over native SQLite to a whole new database library.

I will try Room in my next project and share my experience here in an update later, hope it helps.

Gent
  • 6,215
  • 1
  • 37
  • 40
Farshad
  • 3,074
  • 2
  • 30
  • 44
  • After experimenting with the room, I found out a great concern, the handling the exception from database operations. At present I think there is no way to handle the db exceptions. Also there is no way for the database callbacks such as row id callback on insert , update etc. – SUHAS REKHU May 07 '21 at 06:01