#TECH

Which one to use- Realm or Room?

We have lots of options for storing data. There are plenty of libraries available in the market and some of them look very promising and come out of the box. Today I will highlight some points between two big celebrity names of Android Development in terms of storing data. We will see the difference between the shiny, new library “Room” and the old but famous “Realm”. We will see some of the pros and cons of both libraries but the choice is up to you, which one you will use in your next project. I promise you that at the end of this blog, you’ll have a clear mindset about making a choice between Realm or Room if ever you find yourself at crossroads with them.

Realm vs Room

Let’s talk about what they are and what they do-

REALM

Realm has become favorite among many developers and they are using it without any hesitation. The reason why it is being used so much is that it-
1. Uses standard Java objects
2. Written mostly in C++
3. Simple to create database

The library is well documented and you can use it easily in your project without any prior knowledge of Realm Database. It will guide you at every step like your parents ;).

Here are some points regarding Realm-

  1. Realm works well with Query Builder API.
  2. Realm lets you copy objects so that they can be accessible outside of the Realm instance.
  3. You can only access “Realm” objects when you have Realm instance open, so it means that logic is bound to activity/fragment.
  4. One of the major difference is that Realm was born before Room, so it’s more mature. 🙂
  5. We have also seen that people are facing multi-threading issue in case of Realm.
  6. In case of Realm, all relationships are established using B+ tree. You just need to create your POJO class and declare relationship, Realm abstracts and handles the schema.
  7. Realm can be encrypted.
  8. Realm supports a link between the objects, you can easily access pet.getOwner() if the owner is set as a property of pet.
  9. Each result in case of Realm is lazy-loaded.
  10. Realm doesn’t work with ContentProvider.
  11. There is no support for type adapters, you have to use getter/setter for the data.
  12. Realm is also available for iOS, so if you are planning to build a cross-platform app, Realm will not disappoint you.
  13. Size of the library is 3-4 MB.
  14. As an Android Developer, we can’t ignore the pesky method count limit. Realm has 2k methods in it.

ROOM

Room! Shiniest, newest (a small but famous kid :)) library introduced in the Google IO, 2017 with the Android Architecture Component. Room provides an abstraction layer over SQLite that allows us to use the full power of SQLite. There are three main components in Room-
1. Database
2. Entity
3. DAO

Here are some points regarding Room-

1. Retrofits like interfaces to SQL queries.
2. There is no multi-threading issue in case of Room.
3. You have to establish the relationship if you are using Room, but the reason given by the Room developers doesn’t make it count as a drawback-

However, on the client side, lazy loading is not feasible because it’s likely to happen on the UI thread, and querying information on disk in the UI thread creates significant performance problems. The UI thread has about 16 ms to calculate and draw an activity’s updated layout, so even if a query takes only 5 ms, it’s still likely that your app will run out of time to draw the frame, causing noticeable jank. Worse still, the query could take more time to complete if there’s a separate transaction running in parallel, or if the device is busy with other disk-heavy tasks.

4. Since Room works on SQL, you can write your query easily.
5. Room can be encrypted too.
6. You can use LiveData with Room in a few steps.
7. The library takes up only 50KB.
8. If we consider the pesky method count in case of Room, Room again leads in the race because it only uses 300 methods.
9. Room also provides easy migration mechanism which is fully tested.

So, more or less every storage library does the same thing. Now we have to decide which path we should follow that will suit a particular requirement. If you want to get rid of that large amount of pesky method which is closer to the limit of 64K and also want to play with SQL then you can go with Room. But on the other hand, Realm is not the quickest.

But, oh! it provides you the most suitable version that has 7 years of testing experience. As per my experience if your project has some heavy database work, go for Realm else for small task/project you can opt Room.
I hope now you have some idea of which library fits in which conditions. Happy Coding. 🙂

References

https://developer.android.com/topic/libraries/architecture/room.html
https://academy.realm.io/posts/android-architecture-components-and-realm/?
https://www.reddit.com/r/androiddev/comments/6dj652/realm_vs_room/

 

 

You might also like