Difference of setValue() & postValue() in MutableLiveData

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

    In our app, we had used single LiveData that contains data for multiple views in an activity/screen. Basically N no of datasets for N no of views. This troubled us a bit because the way postData is designed for. And we have state object in LD that conveys to view about which view needs to be updated.

    so LD looks like this:

    LD {
       state (view_1, view_2, view_3 …),
       model_that_contains_data_of_all_views
    }
    

    There are couple of views (view_1 and view_2) that had to be updated when one event occurs..mean they should get notified at the same time when event occurs. So, I called:

    postData(LD(view_1, data))
    postData(LD(view_2, data)
    

    This would not work for reasons we know.

    What I understood is that basically one LD should represent only one view. Then there is no chance that you would've to call postData() twice in a row. Even if you call, the way postData handles it for you is what you would also expect (showing latest data for you in view). Everything fall well in place.

    One LD -> one View. PERFECT

    One LD -> multiple views THERE MAY BE A WEIRD BEHAVIOR

    0 讨论(0)
提交回复
热议问题