简体   繁体   English

Kotlin:ObjectBox和kotlinx.Serialization的组合实体类有冲突吗?

[英]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: 我找不到没有冲突的组合解决方案,因此决定拆分模型:

  1. Each API call for Retrofit 2 改装2的每个API调用
  2. Model for each ObjectBox entity 每个ObjectBox实体的模型

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.

相关问题 java.io.NotSerializableException 带有序列化数据 class in Kotlin (kotlinx.serialization) - java.io.NotSerializableException with serializable data class in Kotlin (kotlinx.serialization) Objectbox 从基类继承属性(它本身不是实体) - Objectbox inherit properties from base class (which is NOT an entity itself) kotlinx.serialization.json.internal.JsonDecodingException:找不到多态序列化程序,因为在 Z0ECD11C1D7A287401D148A23BBD7A2 上缺少 class 鉴别器('null') - kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for missing class discriminator ('null') on JSON Kotlin 项目中未解决的参考 kotlinx - Unresolved reference kotlinx in Kotlin project 有没有办法在 Kotlinx-Serialization 中序列化地图 - Is there a way to serialize a map in Kotlinx-Serialization 序列化逻辑应该在实体还是其他类中 - Should serialization logic be in the entity or other class 如何持久化来自实体 class 的数据并结合数据传输 object? - How to persist data from the entity class and combined with data transfer object? 无法解析 org.jetbrains.kotlinx:kotlinx-gradle-serialization-plugin:0.5.1 - Could not resolve org.jetbrains.kotlinx:kotlinx-gradle-serialization-plugin:0.5.1 引用包含实体管理器的EJB的命名类的序列化 - Serialization of a named class with a reference to EJB, which holds the entity manager kotlin 1.3 协程使用导致 ClassNotFoundException: kotlinx.coroutines.BuildersKt - kotlin 1.3 coroutine usage leads to ClassNotFoundException: kotlinx.coroutines.BuildersKt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM