8

I have a class called Chacara with various properties including thumbList which is a List< String >

When saving it to ROOM I use a Type Converter:

class DbTypeConverter {

    @TypeConverter
    fun listToString(list: List<String>): String = Gson().toJson(list)

    @TypeConverter
    fun stringToList(string: String): List<String> {
        return Gson().fromJson<List<String>>(string, object: TypeToken<List<String>>() {}.type)
    }

}

When I fetch the entire object and access the the thumbList property it works as expected:

@Query("SELECT * FROM chacara WHERE id = :id LIMIT 1")
abstract fun getChacaraWithId(id: String): LiveData<Chacara>

However, when I try to fetch only the thumbList property, the result isn't converted:

@Query("SELECT thumbUrlList FROM chacara WHERE id = :id")
abstract fun getChacaraThumbList(id: String): LiveData<List<String>>

I've also added a log to the TypeConverter to make sure it wasn't being called.

TL;DR: ROOM not calling TypeConverter and not converting String back to List (SELECT 'column_name'), but works when requested the entire object (SELECT *)

Punpuf
  • 2,042
  • 1
  • 19
  • 30
  • I have the same issue and I _think_ it's a bug. I've raised an issue on Room's issuer tracker here: https://issuetracker.google.com/issues/176448260 – Patrick Dec 30 '20 at 23:29

1 Answers1

0

I have the same Problem with my Query. I wanted to select a date, and it didn't work.

@Query("SELECT MAX(downloadDate) FROM table WHERE key=:key")
abstract fun selectMaxDownloadDateByKey(key: String): Date?

Room doesn't know that downloaddate needs to be converted to Date. So I changed the query as follows:

@Query("SELECT MAX(downloadDate) as Date FROM table WHERE key=:key")
abstract fun selectMaxDownloadDateByKey(key: String): Date?

And it works.

So try to change your query to:

@Query("SELECT thumbUrlList as String FROM chacara WHERE id = :id")
abstract fun getChacaraThumbList(id: String): LiveData<List<String>>

I hope it works with LiveData and list results

A.K.
  • 568
  • 5
  • 17