e: 错误: [ObjectBox] Relation targ

2020-02-28  本文已影响0人  _蘇芳_

使用ObjectBox时遇到的迷之问题,看到这个提示,怎么检查那个类都是加了@Entity注解的。
发生原因是:类里定义了ObjectBox不能理解的字段,比如var atIds: MutableList<Long> = mutableListOf(),要写个转换类加上去

    @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
        }
    }

}
上一篇 下一篇

猜你喜欢

热点阅读