Ask Your Question
4

Why is the observer not being triggered when the order/sort of the LiveData List is changed?

asked 2023-06-27 13:50:02 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-27 14:02:01 +0000

huitzilopochtli gravatar image

The observer of a LiveData List is not triggered when the order or sort of the list is changed because LiveData works based on the "identity" of the object, not the contents. In other words, if the observer is observing a LiveData List object and the contents of that object change (i.e., the items in the list are modified or re-ordered), the observer will not be notified because the object itself has not changed.

To work around this, you can create a custom LiveData object that extends the MutableLiveData class and overrides the setValue() method to compare the old and new values of the list and trigger the observer accordingly. Alternatively, you can use a different kind of observer, such as a RecyclerView.AdapterDataObserver or a PagedList.BoundaryCallback, that is specifically designed to handle changes in the contents or ordering of a list.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-27 13:50:02 +0000

Seen: 8 times

Last updated: Jun 27 '23