Allen Vork Allen Vork - 1 year ago 257
Android Question

kotlin data class of RealmObject

I'm using kotlin and Realm to write a data class

data class AuthToken(val register: Boolean,
val token: String,
val tokenSecret: String,
val user: AuthUser)

I have to save the data to db,so i use Realm to save it.But as we know, if i wanna save the class to Realm, the AuthToken class has to extend RealmObject.
That's the problem, kotlin says data class can't extend class.
so i give up data class, just using a normal kotlin class as a model then another question comes:
kotlin class has no getter or setter. As we know realm class have to set all the property private and write getter and setter.
Now i'm wondering how to solve the problem.


Realm doesn't support Data classes currently. You can see an example of how to write Realm compatible model classes in Kotlin here:

public open class Person(
        @PrimaryKey public open var name: String = "",
        public open var age: Int = 0,
        public open var dog: Dog? = null,
        public open var cats: RealmList<Cat> = RealmList(),
        @Ignore public open var tempReference: Int = 0,
        public open var id: Long = 0
) : RealmObject() {