I am using LiveData with Kotlin in my project.
I have been facing an intermittent issue wherein the Observer attached to a MutableLiveData object is sometimes not getting triggered.
The Observer is attached in the activity onCreate(), (with the Activity as the Lifecycle Owner) over the LiveData object in the ViewModel.
ViewModel instantiation:
mViewModel = ViewModelProviders.of(this).get(MyViewModel::class.java)
Setting the observer:
mViewModel.mGoogleDirectionResponse.observe(this, Observer {
it-> drawRouteonMap(it)
})
The LiveData object is being initialised in the Constructor of the ViewModel class.
Note that this is an intermittent issue, and does not happen every time, ie, more than half the time it works exactly as expected.
Is there something I'm doing wrong? Is there a better practice for this implementation that you can recommend?