Realm
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Realmchevron-right

How to migrate from Realm Java SDK to Realm Kotlin SDK

Mohit SharmaPublished May 09, 2022 • Updated May 09, 2022
RealmKotlinJava
Copy Link
facebook icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
This article is targeted to existing Realm developers who want to understand how to migrate to Realm Kotlin SDK.

Introduction

Android has changed a lot in recent years notably after the Kotlin language became a first-class citizen, so does the Realm SDK. Realm has recently moved its much-awaited Kotlin SDK to beta enabling developers to use Realm more fluently with Kotlin and opening doors to the world of Kotlin Multiplatform.
Let's understand the changes required when you migrate from Java to Kotlin SDK starting from setup till its usage.

Changes in setup

The new Realm Kotlin SDK is based on Kotlin Multiplatform architecture which enables you to have one common module for all your data needs for all platforms. But this doesn't mean to use the new SDK you would have to convert your existing Android app to KMM app right away, you can do that later.
Let's understand the changes needed in the gradle file to use Realm Kotlin SDK, by comparing the previous implementation with the new one.
In project level build.gradle
Earlier with Java SDK
With Kotlin SDK, we can delete the Realm plugin from dependencies
In the module-level build.gradle
With Java SDK, we
  1. Enabled Realm Plugin
  2. Enabled Sync, if applicable
With Kotlin SDK,
  1. Replace id 'realm-android' with id("io.realm.kotlin") version "0.10.0"
  1. Remove the Realm block under android tag
  1. Add Realm dependency under dependencies tag
If you are using only Realm local SDK, then you can add
With these changes, our Android app is ready to use Kotlin SDK.

Changes in implementation

Realm Initialization
Traditionally before using Realm for querying information in our project, we had to initialize and set up few basic properties like name, version with sync config for database, let's update them as well.
Steps with JAVA SDK :
  1. Call Realm.init()
  2. Setup Realm DB properties like name, version, migration rules etc using RealmConfiguration.
  3. Setup logging
  4. Configure Realm Sync
With Kotlin SDK :
  1. Call Realm.init()
    Is not needed anymore.
  2. Setup Realm DB properties like db name, version, migration rules etc. using RealmConfiguration- This remains the same apart from a few minor changes.
  3. Setup logging - This is moved to RealmConfiguration
  4. Configure Realm Sync - No changes
Changes to Models
No changes are required in model classes, except you might have to remove a few currently unsupported annotations like @RealmClass which is used for the embedded object.
Note: You can remove Open keyword against class which was mandatory for using Java SDK in Kotlin.
Changes to querying
The most exciting part starts from here 😎(IMO).
Traditionally Realm SDK has been on the top of the latest programming trends like Reactive programming (Rx), LiveData and many more but with the technological shift in Android programming language from Java to Kotlin, developers were not able to fully utilize the power of the language with Realm as underlying SDK was still in Java, few of the notable were support for the Coroutines, Kotlin Flow, etc.
But with the Kotlin SDK that all has changed and further led to the reduction of boiler code. Let's understand these by examples.
Example 1: As a user, I would like to register my visit as soon as I open the app or screen.
Steps to complete this operation would be
  1. Authenticate with Realm SDK.
  2. Based on the user information, create a sync config with the partition key.
  3. Open Realm instance.
  4. Start a Realm Transaction.
  5. Query for current user visit count and based on that add/update count.
With JAVA SDK:
With Kotlin SDK:
Upon quick comparing, you would notice that lines of code have decreased by 30%, and we are using coroutines for doing the async call, which is the natural way of doing asynchronous programming in Kotlin. Let's check this with one more example.
Example 2: As user, I should be notified immediately about any change in user visit info. This is more like observing the change to visit count.
With Java SDK:
With Kotlin SDK :
Again upon quick comparing you would notice that lines of code have decreased drastically, by more than 60%, and apart coroutines for doing async call, we are using Kotlin Flow to observe the value changes.
With this, as mentioned earlier, we are further able to reduce our boilerplate code, no callback hell and writing code is more natural now.

Other major changes

Apart from Realm Kotlin SDK being written in Kotlin language, it is fundamentally little different from the JAVA SDK in a few ways:
  • Frozen by default: All objects are now frozen. Unlike live objects, frozen objects do not automatically update after the database writes. You can still access live objects within a write transaction, but passing a live object out of a write transaction freezes the object.
  • Thread-safety: All realm instances, objects, query results, and collections can now be transferred across threads.
  • Singleton: You now only need one instance of each realm.

Should you migrate now?

There is no straight answer to question, it really depends on usage, complexity of the app and time. But I think so this the perfect time to evaluate the efforts and changes required to migrate as Realm Kotlin SDK would be the future.

Copy Link
facebook icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial
Saving Data in Unity3D Using PlayerPrefs

Sep 07, 2022
Tutorial
Building a Collaborative iOS Minesweeper Game with Realm

May 12, 2022
Article
How to Use Realm Effectively in a Xamarin.Forms App

May 12, 2022
Tutorial
How to Write End-to-End Tests for MongoDB Realm Serverless Apps

Sep 23, 2022
Table of Contents