I've got a couple of `StateFlows` the second depen...
# coroutines
t
I've got a couple of 
StateFlows
 the second depends on the first. But, the second is not emitting when I expect it to..
Copy code
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()
 :
Copy code
override fun bindView(view: PlaylistDetailContract.View) {
    super.bindView(view)

    launch {
        playlist.collect { playlist ->
            foo()
        }
    }
    launch {
        playlistSongs.collect { playlistSongs ->
            bar()
        }
    }
}
m
When 
playlist
 is re-emitted
by
re-emitted
you mean exactly the same value gets emitted?
t
No. The
playlist
object is a new instance and not equal.
playlistRepository.getSongsForPlaylist()
is not called even though
playlist
emits a new, not-equal
playlist
.
m
and does
getSongsForPlaylist
get called for the first time? When first
playlits
value is emitted?
t
Yes
m
ok then, my guess would be the
getSongsForPlaylist
called for the first
playlist
emitted never completes
t
OK. You're probably right! I need to think this over
m
you probably would like to cancel collecting
getSongsForPlaylist
if a new playlist is emitted
t
Do you have any suggestions as to how/where to cancel that?
m
Use
flatMapLatest
instead of
flatMapConcat
💯 1
t
Thanks so much! Been trying to figure this one out for a while!
👍 1