【发布时间】:2017-11-14 09:09:54
【问题描述】:
我目前正在使用 Kotlin 开发一个新的 android 应用程序。我尝试实现 Room 用于数据存储,但我没有让它与 Kotlin 代表一起使用。
我创建了一个Identifier 委托,以确保在初始化后不会更改 id。委托看起来像这样:
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
}
}
我的实体类如下所示:
@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 的情况下以某种方式使其工作吗?
【问题讨论】:
标签: android delegates kotlin android-room