Nemanja Scepanovic
11/25/2021, 10:38 AMjava.lang.ClassCastException: java.lang.String cannot be cast to Query
and I am completely puzzled to why is that?
I get that value classes are “deconstructed” to the primitive type in runtime, but why am I then allowed to use them like this then?
Thanks!Nemanja Scepanovic
11/25/2021, 10:42 AMinterface LocalDataSource {
/**
* Returns [Flow] object containing [Query] objects.
*/
val queryFlow: Flow<Query>
/**
* Sets provided query inside the persistence solution.
*/
suspend fun setQuery(query: Query)
}
class LocalDataStoreImpl(
private val dataStore: DataStore<Preferences>,
) : SearchLocalDataSource {
override val queryFlow: Flow<Query> =
dataStore.data.map { preferences ->
val query = preferences[QUERY_PREFERENCES_KEY] ?: ""
Query(value = query)
}
override suspend fun setQuery(query: Query) {
dataStore.edit { preferences -> preferences[QUERY_PREFERENCES_KEY] = query.value }
}
companion object {
val QUERY_PREFERENCES_KEY = stringPreferencesKey("query-key")
}
}
Nemanja Scepanovic
11/25/2021, 10:43 AMqueryFlow.firstOrNull()
or any other terminal operator crashes with above given exception.rook
12/01/2021, 7:26 PM