Difference of setValue() & postValue() in MutableLiveData

后端 未结 7 1682
故里飘歌
故里飘歌 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:27

    setValue() is called directly from caller thread, synchronously notifies observers and changes LiveData value immediately. It can be called only from MainThread.
    postValue() uses inside something like this new Handler(Looper.mainLooper()).post(() -> setValue()), so it runs setValue via Handler in MainThread. It can be called from any thread.

提交回复
热议问题