I am currently developing a new android app using Kotlin. I tried implementing Room for data storage, but I didn't get it to work with Kotlin delegates.
I created an Identifier delegate in order to ensure the id is not changed after initialization. The delegate looks like this:
class Identifier: ReadWriteProperty<Any?, Long> {
private var currentValue = -1L
override fun getValue(thisRef: Any?, property: KProperty<*>): Long {
if (currentValue == -1L) throw IllegalStateException("${property.name} is not initialized.")
return currentValue
}
override fun setValue(thisRef: Any?, property KProperty<*>, value: Long) {
if (currentValue != -1L) throw IllegalStateException("${property.name} can not be changed.")
currentValue = value
}
}
My entity class looks like this:
@Entity
class Sample {
@delegate:PrimaryKey(autoGenerate = true)
var id by Identifier()
}
When I try to start the app, kapt gives me the following error message:
Cannot figure out how to save this field into database. You can consider adding a type converter for it.
private final com.myapp.persistence.delegates.Identifier id$delegate = null;
Can I somehow get this to work without writing a TypeConverter for every delegate?