Kotlin使用Room persistence lib进行代理

我目前正在开发一个新的Android应用程序使用Kotlin。 我试图实现数据存储的空间,但我没有得到它与科特林代表合作。

我创建了一个Identifier委托,以确保id在初始化后不会被更改。 代表看起来像这样:

 class Identifier: ReadWriteProperty { 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 } } 

我的实体类看起来像这样:

 @Entity class Sample { @delegate:PrimaryKey(autoGenerate = true) var id by Identifier() } 

当我尝试启动应用程序时,kapt给我以下错误信息:

 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; 

我可以以某种方式得到这个工作,而不用为每个代表编写一个TypeConverter