-1

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Were you ever able to resolve this issue? If so, would you please share how? If not, would you please provide more code/detail? – Bink Dec 17 '18 at 23:09

1 Answers1

0

assuming mGoogleDirectionResponse is a LiveData object, it must have an observer. Also you have to call a postValue (or setValue) to trigger the onChange. Unfortunately I can't tell you anything more about your ViewModel because of missing details, but you can already check these points.

Zholoth
  • 86
  • 3
  • mGoogleDirectionResponse is a MutableLiveData object. The observer is set on the object as shown in the code. And yes, I am using postvalue to the object to trigger onChange. – Shashank Saxena Oct 15 '18 at 15:12