0

Here's my ViewModel

class MainViewModel(repository: MainActivityRepo) : ViewModel() {

val isLoading: MutableLiveData<Boolean> = MutableLiveData()

init {
    isLoading.value = false
    android.os.Handler().postDelayed({
        isLoading.value = true
        Timber.d("isCalled")
    }, 5000L)
     }
}

I debugged and checked and the log is working perfectly.

The first value of boolean is set correctly, while the second is not

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77

2 Answers2

1

On background thread, you can use post value instead of set value which will solve your problem!

Vikas Sharma
  • 685
  • 8
  • 18
1

As mentioned by Vikas you should use the postValue() method.

Handler().postDelayed({
    isLoading.postValue(true)
    Timber.d("isCalled")
}, 5000L)
Giorgos Neokleous
  • 1,709
  • 1
  • 14
  • 25