DiffUtils
DiffUtils is an extra functionality that is integrated into the component but is not activated by default. DiffUtil processes each request to update the component's data and then compares the new data with the old data and determines and updates only the items that have truly changed, that way it avoids rendering the entire component.
DiffUtils compares the items at the list level, that is, each item of the data list, new and old.
To get Diffutil up and running, the property must first be enabled.

Next, the ItemComparator and ContentComparator procedures have to be implemented.
By default, the component already has an Item Comparator implementation, which checks if the old item is equal to the new item.
Example
There is a list of data in which each item is a dictionary with an id and name.

Item Comparator must return if the items are equal, by default the component already implements the same mechanism shown in the image. It should only be overridden if a deeper comparison is needed.

The ContentComparator procedure is required since the component does not know the structure of the data list. ContentComparator should return if the content of the old item is equal to the content of the new item.

When DiffUtils is used, it will manage all the changes within the data so methods like NotifyItemChanged, NotifyItemRemoved, NotifyItemInserted cannot be used. It is recommended to use only the UpdateData method.
As explained above, the DiffUtils API is responsible for managing all changes and reflecting them in the component rendering.
Notification blocks will have no effect. To update the rendering of the component, the UpdateData block is used, which can receive the same linked list to process the changes, the API takes care of the rest.
Last modified 11mo ago