Issue
I have searched alot but couldn't get a solution.
I initially got this error
error: Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.
androidx.sqlite.db.SupportSQLiteQuery query);
So after searching a bit, decided to add the @TypeConverter annotation and the @TypeConverters() annotation in the database class
This is the error I am getting now. error: Type converters must receive 1 parameter. public final androidx.sqlite.db.SimpleSQLiteQuery getAllQuery(@org.jetbrains.annotations.NotNull()
My DAO has this -
@Query("SELECT * FROM tea WHERE name == :name")
fun getTea(name: String): LiveData<Tea>
My TypeCoverter class has this method
@TypeConverter
fun getAllQuery(sortBy: TeaSortBy, showOnlyFavorites: Boolean): SimpleSQLiteQuery {
val queryBuilder = SupportSQLiteQueryBuilder
.builder(DataTeaNames.TABLE_NAME)
.orderBy(getSortColumn(sortBy))
if (showOnlyFavorites) {
queryBuilder.selection(DataTeaNames.COL_FAVORITE, arrayOf("1"))
}
return SimpleSQLiteQuery(queryBuilder.create().sql)
}
In the repositroy:
fun getSortedTeas(sort: String, fileByFavorite: Boolean = false): LiveData<PagedList<Tea>> {
val sortBy = SortUtils.TeaSortBy.valueOf(sort)
val factory = dao.getAll(SortUtils.getAllQuery(sortBy, fileByFavorite))
return LivePagedListBuilder(factory, PAGE_SIZE)
.build()
}
Apologies in advance, I am new to this use of TypeConverters in Room Database. I would really appreciate your help. Thank you.
Solution
Turns out it was a mismatch of Kotlin and Room versions that caused my initial error as referred from this answer - https://stackoverflow.com/a/68052960/8442557
Also, I missed adding a @RawQuery annotation.
This solved my error.
Answered By - Saiyan