0

I'm using a ListAdapter for my recyclerView and because I'm working with a mutableList in my viewModel, my recyclerView UI does not update the list if I just pass the same mutableList updated. The simple solution is to type 'it.toList()', but I worry this takes too long creating an completely new list. Is there a better way? Also, explaining how '.toList()' works might help me understand this.

from:

viewModel.basicItems.observe(viewLifecycleOwner) {
    recyclerAdapter.submitList(it)
}

to:

viewModel.basicItems.observe(viewLifecycleOwner) {
    recyclerAdapter.submitList(it.toList())
}
madbeans
  • 99
  • 1
  • 14
  • If you want to understand how toList works, maybe just navigate into the source for it? If you're worried about performance, have you performed any benchmarks? – Doug Stevenson Apr 19 '22 at 04:55
  • 1
    You could also just call `notifyDataSetChanged` to force an update. See [this related question](https://stackoverflow.com/questions/49726385/listadapter-not-updating-item-in-recyclerview) – Tyler V Apr 19 '22 at 04:55
  • @DougStevenson The problem is that if the list becomes too big then it could take significantly longer copy it. I'm looking for a solution that takes up less time. – madbeans Apr 19 '22 at 04:55
  • Less time compared to what? What is the performance benchmark you're trying to achieve? What's the fundamental, observable problem you're trying to solve? You only have a performance problem when you observe it under specific circumstances. – Doug Stevenson Apr 19 '22 at 04:56
  • @TylerV That helped me understand my problem, it looks like .notifyDataSetChanged() is more expensive than .submitList() but maybe submitList() becomes more expensive given enough inputs. I want to know to what extent if any submitList() copying a list becomes too inneficient. – madbeans Apr 19 '22 at 05:10
  • @DougStevenson I have not found problems using submitList() for my current program but since I will use this code in other programs I want to understand it to avoid problems in the future. – madbeans Apr 19 '22 at 05:16
  • 1
    It sounds like premature optimization. If the list gets too large to easily copy in memory then you probably need to restructure more than just this part anyway. I wouldn't worry about it unless it becomes an actual problem. – Tyler V Apr 19 '22 at 12:41

0 Answers0