I have a function to make network calls. It can be called multiple times at the same time and each call runs concurrently.
getDataTask() // it is subscribed on the background thread and observed on the main thread
.subscribe(
{ result -> onResult(result) },
{ onError() }
)
.addToDisposables()
I am able to retrieve the data without any problems. onResult
function updates the MutableLiveData
field in the ViewModel
as
private val _data = MutableLiveData<Data>()
val data: LiveData<Data> get() = _data
private fun onResult(result: Data) = _data.post(result)
And the field is set to be observed in the Fragment
's onViewCreated
method as
viewModel.data.observe(viewLifecycleOwner, Observer { data -> // DO SOMETHING })
When the back-to-back concurrent calls succeed and try to update the _data
field, I noticed that the observer does not observe some of the results. For example, there are 4 concurrent calls start at the same time then they try to post their results and 1 or 2 of the results are not observed. How can I tackle that issue? Any help would be appreciated.