Multiple calls to set LiveData is not observed

后端 未结 5 1040
闹比i
闹比i 2021-01-11 18:28

I have recently seen a weird issue that is acting as a barrier to my project. Multiple calls to set the live data value does not invoke the observer in the view.

It

5条回答
  •  孤城傲影
    2021-01-11 18:46

    FWIW I had the same problem but solved it like this...

    I originally had some code similar to this...

    private fun updateMonth(month: Int){
    updateMonth.value = UpdateMonth(month, getDaysOfMonth(month))
    }
    
    updateMonth(1)
    updateMonth(2)
    updateMonth(3)
    

    I experienced the same problem as described... But when I made this simple change....

     private fun updateMonth(month: Int) {
            CoroutineScope(Dispatchers.Main).launch {
                updateMonth.value = UpdateMonth(month, getDaysOfMonth(month))
            }
        }
    

    Presumably, each updateMonth is going onto a different thread now, so all of the updates are observed.

提交回复
热议问题