55

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.

Mostafa Onaizan
  • 923
  • 1
  • 5
  • 17

3 Answers3

58

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. :)

Dev4Life
  • 2,328
  • 8
  • 22
  • So, if the adapter is linked to Cursor on underlying database or other data provider, we need to facing with this warning anytime when DataSetObserver is triggered ? Nice... – A. Petrov Dec 01 '21 at 07:40
  • yeah..maybe.... – Dev4Life Dec 01 '21 at 16:37
  • What if you removed all the data from your list? Wouldn't `notifyDataSetChanged()` be the fastest way to update the recyclerview? – Micro Jun 05 '23 at 04:34
  • @Micro It will still cause the whole recyclerview rebinding. For large data changing operations, you can use ListAdapter which is same as RecyclerViewAdapter with extra benefits. Check out this example - [ListAdapter Example](https://medium.com/androiddevelopers/adapting-to-listadapter-341da4218f5b) – Dev4Life Jun 05 '23 at 06:41
  • @Dev4Life So you are saying you should use notifyItemRangeChanged() is more efficient if your entire dataset has changed? (in RecyclerViewAdapter) – Micro Jun 05 '23 at 09:55
  • @Micro Yeah, sort of. If you want to change the range of records then you should use `notifyItemRangeChanged()`, and if you really need to change the whole data set then use `notifyDataSetChanged()`. No problem in that. The warning says the same. – Dev4Life Jun 05 '23 at 10:11
30

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)
Mittal Varsani
  • 5,601
  • 2
  • 15
  • 27
  • 1
    wouldn't it automatically know which item has been changed/added/etc if you're using a ListAdapter? with the areItemsTheSame and areContentsTheSame, otherwise I can't see the benefit of ListAdapter – TootsieRockNRoll Sep 23 '21 at 13:49
  • 7
    How about in situation where I don't know how the data has changed? i.e. refreshing the list from the internet? Which method should I use – Joseph Sang Oct 01 '21 at 09:08
4

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.

Bharat Lalwani
  • 1,277
  • 15
  • 17