Realm Blog

Realm + Kotlin just got even better with Realm 4.3.0

We’re excited to announce first class support for Kotlin in Realm Java 4.3.0! 🎉

If you’ve been following us closely, or seen us talk about Kotlin, then you already know that we’ve been busy at work, trying to streamline and simplify Realm Kotlin development.

In 3.6.0, we added additional nullability annotations to avoid leaking Java Platform Types and increase the overall null safety of our API.

Prior to 3.6.0

// Platform types were returned, so..
val result: String? = realm.where( // was legal 
// -- or --
val result: String = realm.where( // WAS ALSO LEGAL!!
// If no results found and null got returned, callers got an IllegalStateException

After 3.6.0

val result: String? = realm.where( // is legal 
// -- but --
val result: String = realm.where( // is illegal!!

Version 3.6.0 also took advantage of the Kotlin type system to infer required fields based on their types.

Prior to 3.6.0

//  An explicit annotation was necessary to indicate a required field, even though it was expressible in the Kotiln type system.
var name: String = ""

After 3.6.0

// Implicitly @Required
var name: String = ""

// Implicitly not @Required
var name: String? = null

What’s new in 4.3.0!

We’ve added several new features for our growing Kotlin user base with this release, including:

Parameterized Functions

Using inline functions w/ reified type parameters, we were able to simplify our API so that when calling createObject, delete, where, you can just pass the type argument in <> and avoid passing the runtime class.

// Instead of this…
val dog = realm.createObject(  // create a Dog
realm.delete( // Delete All Dogs
     .asFlowable() // Fetch all dogs asynchronously as an RxFlowable

// You do this
val dog = realm.createObject<Dog>()  // create a Dog
realm.delete<Dog>() // Delete All Dogs
     .asFlowable() // Fetch all dogs asynchronously as an RxFlowable

// Where Dog implements RealmModel or extends RealmObject.

New Extensions

We’ve added replacements for Realm and RealmObject static utility methods. Wherever your Java code called static Realm.xxxx() and RealmObject.xxxx(), those can be replaced with extension method calls.

The immediately apparent gain here is prettier looking code.

// Given a model class implemented using the model class interface instead of the base class
open class Dog : RealmModel {
  var name: String = "" 

// Instead of 
RealmObject.addChangeListener(dog, changeListener)

// you can do 
dog.addChangeListener() // Because addChangeListener is added as an extension to anything that implements RealmModel.

However, a secondary win here is discoverability of API functions available. Whereas before, you may have had to look at the docs to understand that method RealmModel.xxxx was available, now Android Studio will give you content assist and auto-import the extension for you.

New Keyword

We’ve added the anyOf search term as a replacement for in.

In Java, in order to query for dogs matching any of a list of names, you might do something like this:

RealmResults dogs = realm
             .in("name", new String[] {"Larry","Curly","Moe"})

… but in, is a keyword in Kotlin. So the equivalent query in Kotlin looked like this:

val dogs = realm
     .`in`("name", arrayOf("Larry", "Curly", "Moe"))

… which just looked a bit awkward. So we’ve added a new anyOf keyword, you can use from Kotlin.

val dogs = realm
     .anyOf("age", arrayOf("Larry", "Curly", "Moe"))

Kotlin Model Generation

Finally, we are happy to announce that we’ve added schema model generation back into RealmStudio, our cross platform, next generation version of the Realm Object Browser. In addition to exporting (Swift, C#, JavaScript, and Java), we now also export to Kotlin! 🎉

Great, so how do I use all of this?

That’s the best part! To use all of this Kotlin specific functionality, all you have to do is use Realm with Kotlin! Since Realm gets included in your project as a Gradle Plugin, we can detect when you’re using Kotlin and enable all of this new functionality for you!

At Realm, we want to make persistence as easy as possible, so you can focus on what makes your app unique. Kotlin helps us in that mission.


The Realm Team

Realm Team

At Realm, our mission is to help developers build better apps faster. We provide a unique set of tools and platform technologies designed to make it easy for developers to build apps with sophisticated, powerful features — things like realtime collaboration, augmented reality, live data synchronization, offline experiences, messaging, and more.

Everything we build is developed with an eye toward enabling developers for what we believe the mobile internet evolves into — an open network of billions of users and trillions of devices, and realtime interactivity across them all.

Get more development news like this