[英]Kotlin: combined Entity class of ObjectBox and kotlinx.Serialization in conflict?
I'm using kotlinx.serialization to parse JSON from an API and ObjectBox as a local storage. 我正在使用kotlinx.serialization从API和ObjectBox解析JSON作为本地存储。
Both libraries allow structuring model or entity classes. 这两个库都允许构造模型或实体类。 I'd like to avoid redundant classes and prefer to combine them into one entity class each. 我想避免多余的类,而是希望将它们分别组合成一个实体类。
It worked fine until I had to add relations . 在我必须增加关系之前,它工作正常。
I'd like to accept everything from the API, that's why every field is @Optional
. 我想接受API的所有内容,这就是每个字段都是@Optional
的原因。 The validation is made on the ObjectBox
part of the entity. 验证是在实体的ObjectBox
部分进行的。
Below you can find the desired code, which is not working. 您可以在下面找到所需的代码,该代码不起作用。
1st problem: @Optional
fields need to have a default value. 第一个问题: @Optional
字段需要具有默认值。 After adding ? = null
加入后? = null
? = null
(which felt dirty), I got to the ? = null
(感觉很脏),我到了
2nd problem: after parsing and storing, the following error appears: 第二个问题:解析和存储后,出现以下错误:
java.lang.NoSuchMethodError: No direct method (Lkotlin/reflect/KClass;Lkotlinx/serialization/KSerializer;)V in class Lkotlinx/serialization/ContextSerializer; java.lang.NoSuchMethodError:没有直接方法(Lkotlin / reflect / KClass; Lkotlinx / serialization / KSerializer;)V in Lkotlinx / serialization / ContextSerializer类; or its super classes (declaration of 'kotlinx.serialization.ContextSerializer' appears in /data/app/com.acme.myapp2A1fXerVu==/split_lib_dependencies_apk.apk) 或其上级类(“ kotlinx.serialization.ContextSerializer”的声明出现在/data/app/com.acme.myapp2A1fXerVu==/split_lib_dependencies_apk.apk中)
Entity: AppleTree 实体:AppleTree
@Entity // ObjectBox
@Serializable // Serializable
data class AppleTree(
@Optional // Serializable
@Id(assignable=true) // ObjectBox
var id: Long = 0,
@Optional // Serializable
@Backlink // ObjectBox
var apples: ToMany<Apple>
)
Entity: Apple 实体:苹果
@Entity
@Serializable
data class Apple(
@Optional // Serializable
@Id(assignable=true) // ObjectBox
var id: Long = 0,
@Optional // Serializable
var tree: ToOne<AppleTree>
)
Fetch & store 提取并存储
private fun fetch() {
doAsync {
var json = mapKeys(URL(myURL).readText()) // fetch JSON from API
uiThread {
val appleTree = JSON.nonstrict.parse<AppleTree>(json) // parse JSON
val appleTreeBox = (application as App).getAppleTreeBox() // get ObjectBox
appleTreeBox.put(appleTree) // save
}
}
}
Any idea? 任何想法?
Thank you in advance! 先感谢您!
I could not find a combined solution without conflicts, so I decided to split the models: 我找不到没有冲突的组合解决方案,因此决定拆分模型:
I create ObjectBox entities after fetching the data from Retrofit. 我从Retrofit中获取数据后创建ObjectBox实体。 A bit too much redundancy, but it works. 冗余太多了,但是可以用。 :-) :-)
I just saw this solution and wrote an article about how to do this simply without mapping Entities. 我刚刚看到了此解决方案,并写了一篇文章,介绍如何在不映射实体的情况下简单地做到这一点。
I hope this might assist you -> https://medium.com/@ryan.shaw_80293/android-local-database-tricks-with-kotlin-and-objectbox-f0da58c9d5a3 希望对您有所帮助-> https://medium.com/@ryan.shaw_80293/android-local-database-tricks-with-kotlin-and-objectbox-f0da58c9d5a3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.