Querying room database with List of Enums as an argument yields results only when a single Enum is provided in the list and returns no results when multiple Enums in list. Given a list of a single Enum in the query the resuls returned will only be those that match exactly that Enum and no results that consist of that Enum and other Enums combined.
However, according to this post results should be returned that match a containing element. Additionally, android documentation outlines the expected behaviour to be the same as far as i understand. I found this issue tracked which is of a very similar nature, albeit, not exact which led to a merge in AOSP but has provided little insight into my issue.
Any help appreciated, code below:
Enum
enum class Type(val type: String) {
NORMAL("normal"),
FIGHTING("fighting"),
FLYING("flying"),
POISON("poison"),
GROUND("ground"),
ROCK("rock"),
BUG("bug"),
GHOST("ghost"),
FIRE("fire"),
WATER("water"),
GRASS("grass"),
ELECTRIC("electric"),
PSYCHIC("psychic"),
ICE("ice"),
DRAGON("dragon"),
FAIRY("fairy"),
STEEL("steel");
}
Dao
@Query("SELECT * from pokemon_table WHERE types IN (:typesList)")
fun getFilteredPokemon(typesList: List<Type>): LiveData<List<Pokemon>>
Type Converter
@TypeConverter
fun fromString(value: String?): List<Type>? {
val listType = object : TypeToken<List<Type>>() {}.type
return Gson().fromJson(value, listType)
}
@TypeConverter
fun fromList(list: List<Type>?): String? {
val listType = object : TypeToken<List<Type>>() {}.type
return Gson().toJson(list, listType)
}
Here is a shot of the database to illustrate the values stored, querying with listOf(Type.FIRE)
will return charmeleon as a result, but not charizard.