amitshekhariitbhu / MVVM-Architecture-Android

MVVM architecture using Kotlin, Dagger, Retrofit, Coroutines, Flow, StateFlow, and etc.
https://outcomeschool.com/blog/mvvm-architecture-android
567 stars 121 forks source link

How to use MVVM for Recycleview loadmore action #5

Open est7 opened 1 year ago

est7 commented 1 year ago

Thank you for your article. I would like to know, according to Android Official Guide of Unidirectional Data Flow, should I call uiState.Success(newAllList) when I pull more data, or add a new state: uiState.SuccessLoadMore(appendList)?

In the first case, I need to use DiffUtil ,If I have a large list but only want to append 20 pieces of data to it, is it not worth the loss? I prefer to use the second method and then call adapter.notifyItemRangeInserted(). I am very confused about this. How do you recommend I do it? Thank you very much for your answer.

07NG commented 1 year ago

It is not worth using DiffUtil if you have a huge list but just want to append 20 bits of data to it. To update the adapter in this scenario, just use the notifyItemRangeInserted() function.

If you are often adding data to the list, though, DiffUtil may be a better solution. This is due to DiffUtil's efficiency over manually changing the adapter.

Finally, if you are simply infrequently adding data to the list, the notifyItemRangeInserted() function should suffice. If you are often adding data to the list, though, DiffUtil may be a better solution.

est7 commented 1 year ago

@07NG I prefer using the notify method, but now we promote using a single data source. In order to drive the UI display, I should hold this single data source in the viewmodel. If I want to perform an action like uistate.copy(list = list + appendlist), then I cannot use the 'notify' method. This notify method resembles the code smell of imperative programming.