Realm documentation in Kotlin


#1

Now that Kotlin is the official modern language for Android, could you add a Kotlin documentation similar to how there is for Swift? There are many things that have to be specified differently due to Kotlin/Swift’s non-nullability and readonly-ness – so for example using @LinkingObjects( … ) in Kotlin gives the error “@LinkingObjects field “abc.models.Foo.bar” must be final”. It can take quite a bit of trial/error/research to find out how to get around this in Kotlin – would be incomparably better if the official Realm docs instead showcased the way.


#2

You mean you would like our examples in the doc section to show both Kotlin and Java?


#3

Good point, mbradea. It does take trial and error to get it working. For example, your RealmModel Kotlin classes must be declared open in order to work with Realm.

I have not had an issue getting up and running as it is pretty quick to learn the ropes with Kotlin and Realm but the docs can always get better, right?

If you need help on specific topics, please reach out. I myself have been only writing in Kotlin for past 1 1/2 years with Realm and love it. Happy to help.


#4

well “must be made final” means that it should be declared as val

Otherwise, the other fields must all be var, and the class must be open, and if there are constructor parameters then they must all have default value.


#5

Hi, I’m a newbie to Realm & Kotlin building an Android version of my iOS app.
I’ve started by building a simple project to understand the basics of Realm.
I have got completely stuck with the 2 following error messages and the course tutor I’m following has been unable to work out what the problem is.

  1. Cause: io.realm.com_xxxx_realmtest_DogRealmProxyInterface
  2. This Realm is not part of the schema.
    Which one of these errors seems to depend which order I put this lines in the build.app.
    apply plugin: ‘kotlin-android’
    apply plugin: ‘realm-android’
    My main activity and Dog.kt file are below.
    Hopefully you can point me in the right direction here. Thanks in advance if you are able to look at this.

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import io.realm.Realm
import io.realm.RealmObject

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val realm = Realm.getDefaultInstance()
    realm.beginTransaction()
    val myDog = realm.createObject(Dog::class.java)
    myDog.name = "Fido"
    myDog.age = 10
    realm.commitTransaction()

    val query = realm.where(Dog :: class.java)
    val results = query.findAll()
    for (dog in results) {
        println(dog.name)
    }

}

import io.realm.RealmObject
import io.realm.RealmModel

open class Dog : RealmObject() {
var name = “”
var age = 0

}


#6

You need the plugins in the order specified here: https://realm.io/docs/java/latest/#kotlin
You can also take a look at our Kotlin example here: https://github.com/realm/realm-java/tree/master/examples/kotlinExample


#7

Thanks thats cleared things up a bit.
I now just get the error;
io.realm.exceptions.RealmException: Dog is not part of the schema for this Realm

I notice that in my MainActivity.kt the import of Dog is grey and unused even though I have the line
val myDog = realm.createObject()


#9

I think the point made in the main post on this thread is good and still valid. I initially started using Realm with Swift, and I found that the documentation was pretty good and I was able to find some solid examples to work from. Using Realm with Kotlin has been a lot more frustrating.

Dedicated Kotlin docs would be helpful. I’d really like to see some more complex example projects though. The kotlin example git is very simple.