https://kotlinlang.org logo
#coroutines
Title
# coroutines
t

Tim Malseed

07/16/2020, 3:37 AM
I’ve got the following code (Android):
Copy code
coroutineScope.launch {
    val songs = mutableListOf<Song>()
    mediaProvider.findSongs().collect { (song, progress) ->
        songs.add(song)
        listeners.forEach { listener -> listener.onProgress(progress, song) }
    }

    listeners.forEach { listener -> listener.onComplete() }
}
The scope is instantiated via
CoroutineScope(Dispatchers.Main)
Despite
listener.onProgress
being called on the main thread - and successful log messages when it is called, the UI updates I’m making in response don’t seem to have any effect. Are the
listener.onProgress(progress, song)
calls somehow being gathered up and then all executed at the end? Ooh I think I might be blocking the main thread somewhere
o

octylFractal

07/16/2020, 3:39 AM
unless you're `suspend`ing somwhere, you are still blocking the Main thread -- I presume
findSongs()
is a Flow, but did you move it off of the main thread while it works?
t

Tim Malseed

07/16/2020, 3:40 AM
Huh. You know, I did have a
.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
call, but I’ve accidentally removed it at some point. That will be it!
Its taken me about 2 hours of debugging to come to write this post and then as soon as I did I realise I must be blocking the main thread
rubber duck 1
Thanks @octylFractal
👍 1
That was it 🙂
4 Views