Hello guys I am having problem with my synchronous...
# coroutines
j
Hello guys I am having problem with my synchronous call, my use case is I wanted to finish all collection before launching
doAnotherCall()
, the
viewModel.detailsState
collects in a loop. I mean its
emit and collect
in a loop adding delay in
doAnotherCall()
does not help. Best Regards.
Copy code
private suspend fun collectProductList() = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
    viewLifecycleOwner.lifecycleScope.launch {
        viewModel.detailsState.collectLatest { state ->
            when (state) {
                is ItemDetailState.ShowLoader -> binding.progressBar.visible()
                is ItemDetailState.HideLoader -> binding.progressBar.gone()
                is ItemDetailState.OnSuccess -> handleSubListSuccess(state.list, state.details)
                is ItemDetailState.OnFailed -> handleFailure(state.error)
            }
        }
    }
    launch {
        doAnotherCall()
    }
}
Solve it by adding observable and emit the counter in every onSuccess then add logic if
list size ==  counter size
then call doAnotherCall instead of combining it to one scope.
j
What about just removing the 2 launch calls? If you want to do one thing and then the other, just do these 2 things without launching coroutines
j
Let me try and will let you know thanks.
j
You will of course have to make sure that the flow you collect is actually finite