I have problem with collection songs from my database.
Song Dao:
@Query("SELECT * FROM song_table")
fun observeSongs() : Flow<List<DatabaseSongListItem>>
Repository:
fun observeSongs() = songDao.observeSongs()
ViewModel:
private val _observeSongs = MutableStateFlow(emptyList<DatabaseSongListItem>())
val observeSongs = _observeSongs.asStateFlow()
viewModelScope.launch {
songListRepository.getSongs() -> this line can get api songs + it add songs to database
songListRepository.observeSongs().collect(){ songs ->
_observeSongs.value = songs
}
}
MainActivity:
lifecycleScope.launch {
viewModel.observeSongs.collect {
Log.d(ContentValues.TAG, "MAIN: $it")
}
This code is working but im collecting my songs 2 times, 1 in viewModel and another one i activiy. I only want to collect it in my activity once. But when I use this line of code in viewModel I get this:
_observeSongs.value = songListRepository.observeSongs()
Type mismatch.
Required:
List<DatabaseSongListItem>
Found:
Flow<List<DatabaseSongListItem>>
I want to show list of songs in recyclerView that is why I need List of songs.