e: 错误: [ObjectBox] Relation target class 'Long' defined in class 'LocalMessage' could not be foun...

使用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
        }
    }

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容