0

I want to run a function in a background thread with Dispatcher.IO on recycleView code like this

class FileAdapter(val isFromNetwork : Boolean, val context: Context, val tools: Tools, val coroutineScope: CoroutineScope):RecyclerView.Adapter<FileAdapter.FileHolder>() {
    ...
    override fun onBindViewHolder(holder: FileHolder, position: Int) {  
        coroutineScope.launch{
        val task =async(Dispatchers.IO){ tools.networkFileManager(networkFileList[position]) }
        task.await().let{
            //some view function
        }
    }
    ...
}

with code like that the tools.networkFileManager function runs in the background thread at the same time. how to get tools.networkFileManager called in the background thread sequentially? (next thread waiting for the previous thread to finish)

HT__
  • 11
  • 2
  • You need to run it sequentially in relation to what exactly? Above code is currently sequential. Do you mean that `onBindViewHolder()` can be executed multiple times, but only a single `networkFileManager()` can run at a time? – broot Jun 22 '22 at 06:28
  • Try to use `MutableSharedFlow` or `Dispatchers.IO.limitedParallelism(1)` https://stackoverflow.com/a/72332468/1731626 – Sergio Jun 22 '22 at 06:29
  • Solved using MutableSharedFlow, thanks everyone – HT__ Jun 22 '22 at 07:03

0 Answers0