Skip to content Skip to sidebar Skip to footer

How To Make A Typeconverter In Room From A Custom List?

I have made a TypeConverter but I get an error Unable to create converter for class .models.lastanime.EpisodesEntityfor method EpisodesApi.getEpisodes I can't finish understand

Solution 1:

You cannot have an entity holding a List of another entity. You need to define a one-to-many relation between them.

Solution 2:

After several days carefully studying more about the advanced inserts of Room, I have discovered how to make the TypeConverter for a specific custom object, In my case ServersEntity

@TypeConverterfunstringToListServer(data: String?): List<ServerEntity?>? {
    if (data == null) {
        return Collections.emptyList()
    }
    val listType: Type = object :
        TypeToken<List<ServerEntity?>?>() {}.type
    return gson.fromJson<List<ServerEntity?>>(data, listType)
}

@TypeConverterfunlistServerToString(someObjects: List<ServerEntity?>?): String? {
    return gson.toJson(someObjects)
}

On the other hand to convert the String lists, it would simply be done as follows

@TypeConverterfunfromString(value: String?): List<String> {
    val listType = object :
        TypeToken<ArrayList<String?>?>() {}.type
    return Gson().fromJson(value, listType)
}

@TypeConverterfunfromList(list: List<String?>?): String {
    val gson = Gson()
    return gson.toJson(list)
}

Post a Comment for "How To Make A Typeconverter In Room From A Custom List?"