Help with coroutines


#1

So here is all the relevant infos

  • This is a jukebox application
  • The view is refreshed every frames.
  • There is background music playing
  • There is a function that walk a directory, adding mp3 files and images to a list of items

The problem is that, the items that this function add to the list are very heavy (contains music and image files) so when I call this function, the background music freezes for a sec until the loading is finished.

The function looks something like that (on top of my head)
fun dirItems (path: String): ArrayList {
//Heavy stuff (load images, musics, add items to list etc…)
}

So how can I make it so it runs in background and doesn’t interrupt the music? There is kind of a lot of stuff we can do with coroutines (suspend, launch, runBlocking, async etc…) and I’m not familiar enough to know what utilities I have to take


#2

The coroutine’s DefaultDispatcher is intended for tiny-non blocking piece of code, so probabily use it for this purpore has something wrong.

For a long running job I suggest you to use a dedicated thread, with daemon=true and priority=MIN_PRIORITY.