Tim Malseed
06/08/2021, 4:40 AMStateFlows
the second depends on the first. But, the second is not emitting when I expect it to..private val playlist = playlistRepository.getPlaylists(PlaylistQuery.PlaylistId(initialPlaylist.id))
.map { playlists ->
playlists.firstOrNull()
}
.filterNotNull()
.stateIn(
scope = this,
started = SharingStarted.WhileSubscribed(),
initialValue = initialPlaylist
)
private val playlistSongs: StateFlow<List<PlaylistSong>> = playlist
.flatMapConcat { playlist ->
playlistRepository.getSongsForPlaylist(playlist.id, playlist.sortOrder)
}
.stateIn(
scope = this,
started = SharingStarted.WhileSubscribed(),
initialValue = emptyList()
)
playlistSongs
depends on playlists
. When playlist
is re-emitted, I expect playlistSongs
to flatmap the new playlist
, and potentially emit a new set of playlistSongs
But the flatMapConcat
block in playlistSongs
doesn't seem to get called, even after playlist
emits.
Both flows are collected in bindView()
(called in Fragment.onViewCreated()
:
override fun bindView(view: PlaylistDetailContract.View) {
super.bindView(view)
launch {
playlist.collect { playlist ->
foo()
}
}
launch {
playlistSongs.collect { playlistSongs ->
bar()
}
}
}
mateusz.kwiecinski
06/08/2021, 4:47 AMWhenbyis re-emittedplaylist
re-emitted
you mean exactly the same value gets emitted?Tim Malseed
06/08/2021, 4:47 AMplaylist
object is a new instance and not equal.playlistRepository.getSongsForPlaylist()
is not called even though playlist
emits a new, not-equal playlist
.mateusz.kwiecinski
06/08/2021, 4:49 AMgetSongsForPlaylist
get called for the first time? When first playlits
value is emitted?Tim Malseed
06/08/2021, 4:49 AMmateusz.kwiecinski
06/08/2021, 4:50 AMgetSongsForPlaylist
called for the first playlist
emitted never completesTim Malseed
06/08/2021, 4:51 AMmateusz.kwiecinski
06/08/2021, 4:51 AMgetSongsForPlaylist
if a new playlist is emittedTim Malseed
06/08/2021, 4:53 AMmateusz.kwiecinski
06/08/2021, 4:54 AMflatMapLatest
instead of flatMapConcat
Tim Malseed
06/08/2021, 4:55 AM