使用ObjectBox
時遇到的迷之問題碑隆,看到這個提示砌滞,怎么檢查那個類都是加了@Entity
注解的。
發(fā)生原因是:類里定義了ObjectBox
不能理解的字段语盈,比如var atIds: MutableList<Long> = mutableListOf()
,要寫個轉(zhuǎn)換類加上去
@Convert(converter = LongList2StringConverter::class, dbType = String::class)
var atIds: MutableList<Long> = mutableListOf()
)
class LongList2StringConverter : PropertyConverter<MutableList<Long>, String> {
override fun convertToDatabaseValue(entityProperty: MutableList<Long>?): String {
return if (entityProperty.isNullOrEmpty()) {
"[]"
} else {
val jsa = JSONArray()
entityProperty.forEach {
jsa.put(it)
}
jsa.toString()
}
}
override fun convertToEntityProperty(databaseValue: String?): MutableList<Long> {
val result = mutableListOf<Long>()
return if (databaseValue.isNullOrEmpty()) {
result
} else {
val jsa = JSONArray(databaseValue)
for (i in 0 until jsa.length()) {
result.add(jsa[i] as Long)
}
result
}
}
}