本文介绍了TypeConverters无法弄清楚如何将该字段保存到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现Room的类型转换器时出现错误.

I got an error when implementing the type converter of Room.

这是我的实现,首先像下面这样配置实体,因为POJO包含Map列表,所以我实现了Room的TypeConverter

This is my implementation, Firstly config the entity like below, because the POJO contain List of Map I implemented the Room's TypeConverter like this

object OtherServicesTypeConverter {
    private val gson = Gson()
    @TypeConverter
    @JvmStatic
    fun stringToList(data: String?): List<Map<String, OtherServiceEntity>> {
        if (data == null) {
            return Collections.emptyList()
        }

        val listType = object : TypeToken<List<Map<String, OtherServiceEntity>>>() {

        }.type

        return gson.fromJson<List<Map<String, OtherServiceEntity>>>(data, listType)
    }

    @TypeConverter
    @JvmStatic
    fun listToString(objects: List<Map<String, OtherService>>): String {
        return gson.toJson(objects)
    }
}

然后在实体和数据库中使用注释,如下所示:

Then using annotation inside the entity, and database as below

@Entity(tableName = "ProfileEntities")
    data class ProfileEntity(
            @PrimaryKey
            @SerializedName("masterId") var masterId: Long = 0,
            @SerializedName("nickname") var nickname: String = "",
            @TypeConverters(OtherServicesTypeConverter::class)
            @SerializedName("otherServices") var otherServices: List<Map<String, OtherServiceEntity>>,
            @Embedded
            @SerializedName("images") var images: ProfileImagesEntity = ProfileImagesEntity(),
            @SerializedName("updatedAt") var updatedAt: String = ""
    )


 @Database(entities = [ArticleFeatureEntity::class, ProfileEntity::class], version = 3)
    @TypeConverters(DataTypeConverter::class, ProfileTypeConverter::class, OtherServicesTypeConverter::class)
    abstract class AppDatabase : RoomDatabase() {
        abstract fun profileDao(): ProfileDao
        // Code here


    }

请帮助支持我,谢谢!

推荐答案

最后,我认识到Room需要在类型转换器实现中进行一些特殊更改(更改为ArrayList<HashMap<String, OtherServiceEntity>>).尽管如此,还是不​​知道这是一个功能或错误,因为在其他情况下,我仍然可以使用List作为类型.这就是我所做的细节,现在我可以运行它了

Finally, I recognized that Room needs some special change in type converter implementation (change to ArrayList<HashMap<String, OtherServiceEntity>>). Still, don't know it is a feature or bug because other cases I am still able to use List as type. This is the detail what I did, and now I am able to run it

@Entity(tableName = "ProfileEntities")
data class ProfileEntity(
        @TypeConverters(ProfileTypeConverter::class)
        @SerializedName("details") var details: List<ProfileDetailEntity> = listOf(),
        @Embedded
        @SerializedName("images") var images: ProfileImagesEntity = ProfileImagesEntity(),
        @PrimaryKey
        @SerializedName("masterId") var masterId: Long = 0,
        @TypeConverters(OtherServicesTypeConverter::class)
        @SerializedName("otherServices") var otherServices: ArrayList<HashMap<String, OtherServiceEntity>> = arrayListOf(),
        @SerializedName("updatedAt") var updatedAt: String = ""
)

以及转换器的实现

class ProfileTypeConverter {
    private val gson = Gson()
    @TypeConverter
    fun stringToList(data: String?): List<ProfileDetailEntity> {
        if (data == null) {
            return Collections.emptyList()
        }

        val listType = object : TypeToken<List<ProfileDetailEntity>>() {

        }.type

        return gson.fromJson<List<ProfileDetailEntity>>(data, listType)
    }

    @TypeConverter
    fun listToString(someObjects: List<ProfileDetailEntity>): String {
        return gson.toJson(someObjects)
    }
}

class OtherServicesTypeConverter {
    private val gson = Gson()
    @TypeConverter
    fun stringToList(data: String?): ArrayList<HashMap<String, OtherServiceEntity>> {
        if (data == null) {
            return ArrayList()
        }

        val listType = object : TypeToken<ArrayList<HashMap<String, OtherServiceEntity>>>() {

        }.type

        return gson.fromJson<ArrayList<HashMap<String, OtherServiceEntity>>>(data, listType)
    }

    @TypeConverter
    fun listToString(objects: ArrayList<HashMap<String, OtherServiceEntity>>): String {
        return gson.toJson(objects)
    }
}

祝您编程愉快!

这篇关于TypeConverters无法弄清楚如何将该字段保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 12:06