Difference of setValue() & postValue() in MutableLiveData

后端 未结 7 1684
故里飘歌
故里飘歌 2020-11-28 05:34

There are two ways that make change value of MutableLiveData. But what is difference between setValue() & postVa

7条回答
  •  悲哀的现实
    2020-11-28 06:17

    Based on the documentation:

    setValue():

    Sets the value. If there are active observers, the value will be dispatched to them. This method must be called from the main thread.

    postValue():

    Posts a task to a main thread to set the given value. If you called this method multiple times before a main thread executed a posted task, only the last value would be dispatched.

    To summarize, the key difference would be:

    setValue() method must be called from the main thread. But if you need set a value from a background thread, postValue() should be used.

提交回复
热议问题