It will always be more efficient to use more specific change events if you can. Rely on notifyDataSetChanged as a last resort. RecycleView

Viewed 19100

after update android studio to arctic fox, I get this warning. But I dont know what is the efficient way to notify data change. in my code I'm filling the adapter from network call and then I notifydatasetchange, but the compiler gave me this:

It will always be more efficient to use more specific change events if you can. Rely on notifyDataSetChanged as a last resort. RecycleView

edit question: the want us to use

DiffUtil docs

instead of notifyDataSetChanged() because it much faster. check this article on medium.

3 Answers

It means that if you need to change the whole item list at once in the recyclerview, then use notifyDataSetChanged().

If you need to change the specific item, then it's better to use notifyItemChanged(position) so that it won't refresh & rebind the whole dataset which can impact the performance if the dataset is large.

So it's just a normal suggestion or maybe a warning, nothing to worry about. :)

The function notifyDataSetChanged essentially considers all data in your dataset has changed. This causes all VISIBLE views using this data to be redrawn. This is unnecessary when only some data has changed.

You need to identify the position that data has change and notify your adapter to update only those items.

you can notify change of the particular position using this methods

  1. notifyItemChanged(int)
  2. notifyItemInserted(int)
  3. notifyItemRemoved(int)
  4. notifyItemRangeChanged(int, int)
  5. notifyItemRangeInserted(int, int)
  6. notifyItemRangeRemoved(int, int)

It's just a suggestion by Android studio to update data in RecyclerView.

This advice to notify change in the Recycler view items using particular postion update only like, notifyItemChanged(int), notifyItemInserted(int), notifyItemRemoved(int) ,notifyItemRangeChanged(int, int), notifyItemRangeInserted(int, int), notifyItemRangeRemoved(int, int).

notifyDataSetChanged() should be used as the last resort or as the final course of action. This function will reinitialize and rebind all views once again which can decrease the performance.

Related