DiffUtil — Game Changer Of Recycler View Performance

Swatiomar
3 min readApr 18, 2021

Love of learning is the most necessary passion … In it lies our happiness. It’s a sure remedy for what ails us, an unending source of pleasure
— Emilie du Chatelet

We all are using Recycler view to show the list of items in app. But when the time comes to update the data then we just called notifyDataSetChanged() which is very costly operation because it updates the whole view. So here, DiffUtil comes into the picture, it just update those views that were actually changed…

DiffUtil is a utility class that calculates the difference between two lists and outputs a list of update operations that converts the first list into the second one. It uses Eugene W. Myers’s difference algorithm to calculate the minimal number of updates to convert one list into another.

How to use DiffUtil?

A Callback class used by DiffUtil while calculating the diff between two lists. It is an abstract class and It has 4 abstract methods and 1 non abstract method. When you extend this class you have to override methods that are as follows:

getOldListSize() : return the size of old list

getNewListSize() : return the size of new list

areItemsTheSame(int oldItemPosition, int newItemPosition) : return boolean. It checks whether two objects represent same item. e.g. if your items have unique ids, this method should check their id equality.

areContentsTheSame(int oldItemPosition, int newItemPosition) : return boolean. It Called by the DiffUtil when it wants to check whether two items have the same data. DiffUtil uses this information to detect if the contents of an item has changed.

getChangePayload(int oldItemPosition, int newItemPosition) : This one is non abstract method. When areItemsTheSame(int, int) returns true for two items and areContentsTheSame(int, int) returns false for them, DiffUtil calls this method to get a payload about the change. Object returned in getChangePayload() is dispatched from DiffResult using notifyItemRangeChanged(position, count, payload), upon which is called Adapter’s onBindViewHolder(… List<Object> payloads) method.

Once DiffUtil.Callback implementation is done, you have to update the list change in RecyclerViewAdapter as described below-

val diffCallback = EmployeeDiffCallback(this.dataList, dataList)
val diffResult: DiffUtil.DiffResult = DiffUtil.calculateDiff(diffCallback)
this.dataList?.clear()
dataList?.let { this.dataList?.addAll(it) }
diffResult.dispatchUpdatesTo(this)

In above, calculateDiff calculates the list of update operations that can covert one list into the other one. and this returns result of difference. You can consume the updates in a DiffResult via dispatchUpdatesTo(ListUpdateCallback) which dispatch the update events to given adapter and adapter will notify the change.

If the lists are large, this operation may take significant time so you are advised to run this on a background thread, get the DiffResult then apply it on the RecyclerView on the main thread. Due to implementation constraints, the max size of the list can be ²²⁶.

To get better understanding and coding part, Please go through GitHub link:
GitHub Link : https://github.com/swatiomar27/Game-changer-of-Recycler-View-DiffUtil
Demo App Link: https://youtu.be/D03jQInelV4

Happy Coding :)

I hope you liked the article. Feel free to share any feedback. And stay tuned for more concepts!

Don’t forget to follow me on Medium. If you liked the article, click the clap below so more people can see it!

Thanks !!!

--

--

Swatiomar

As a programmer, to keep learning and help ones who needs the skills to be a master, I am available to be a part of an adventurous journey….