Image for post
Image for post
“stocked yarns” by Alex Block on Unsplash

Things to remember when multithreading Realm DB

Realm is one of the finest alternatives to SQLite DB we have in Android. It is really easy to set up and fun to work with. Since realm lazy loads the data it is really fast to read.

Realm has its own pain points the biggest one being the multithreading. The realm managed objects (the objects that read from realm) cannot be passed across threads unless you convert them to POJO. The writes to the realm are painfully slow in certain cases(when you have listeners to the item you are changing).

Here are some of the things to remember when using realm in multiple threads

Async Queue has a max size of 100

It's pretty easy to write something to realm in async using executeTransactionAsync method like this

Image for post
Image for post

Here is the catch. There is a limit to the number of items that can be added to this transaction queue. If the number of transaction added to the queue is more than 100 the Realm DB will throw an error. So if you are writing in a lot of items in one go, make sure you are writing in a single transaction and not adding it as different transactions.

Image for post
Image for post

The above method is not encouraged. It is better to write in a single transaction.

Image for post
Image for post

Need for a handler Thread

For concurrency control, Realm uses MVCC(Multi-version Concurrency Control). Each thread gets a snapshot of the DB at that particular point of time when the realm instance created. When there is a change in the data the snapshot is refreshed and you get the latest data. But the snapshot is refreshed only if the thread has a looper. If you have a realm instance on Thread A which does not have a looper and a new entry is written to the DB from Thread B. The realm instance in thread A will not be able to see the new data.

So if you have a realm instance that is going to be open for a long time it is better to be on a handler thread.

If you by any chance need to have a realm instance on a non-looper thread and you want to refresh the snapshot of the DB then you can use realm.refresh() to update the realm snapshot.

Having Multiple Instances

When you are using multiple instances of realm running on multiple threads you have to understand the cost associated with it. When you create a new instance of realm it takes up a lot of memory. So if you do not close the realm after you are done with it or you have a large number of threads using realm at the same time. There is a chance that you might end up in out of memory exception. Hence it is better to have a limited number of open realm instance at any given point in time.

Realm.getGlobalInstanceCount(Realm.getDefaultConfiguration()) can be used to find the number of active realm instance.

Always use try with resources in Java or use in kotlin to close the realm instance in after use.

Image for post
Image for post

The above mentioned are some of the things to remember when you are using realm in a multithreading environment.
~Happy coding~

Android dev @ hotstar

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store