I got an error when implementing the type converter of Room.
error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
private java.util.List<? extends java.util.Map<java.lang.String, mypage.profile.entity.OtherServiceEntity>> otherServices;
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
}
Please help to support me, thank you!