How to handle a lot of api requests and observe them in kotlin Coroutines

Viewed 312

I want to call a request to an API in a loop and observe the result for each api requests. The problem is that I think some result are got lost. How can I handle this? I want to handle this using flow or stateflow. Is it better to wait to get result from a request then proceed to another number in the loop? How? Or is there a coroutines function that pauses a loop till the result is observed?

lifecycleScope.launchWhenCreated {

        for (i in 1..100) {
            viewModel.apiRequest(i)
        }
}
lifecycleScope.launchWhenCreated {

       viewModel.resultStateFLow.observe {
  // update ui or ..
    }
}

0 Answers
Related