Currently I am using Android Architecture Components for App development everything is working along with paging library Now I want to remove recyclerview Item using PagedListAdapter to populate this we required to add a data source and from data source list is updating using LiveData no I want to remove a item from list notifyItemRemoved() is working from PagedList I am getting this exception:
java.lang.UnsupportedOperationException
java.util.AbstractList.remove(AbstractList.java:638)
I had same issue.
@JiajunWong did you find any solution?
i got this problem to when i try to Collections.sort
I have created some custom adapter to handle this case(only a part of it actually). Luckily I was supposed to add on top of current list, so following solution only supports adding to top. However you can try to customize it.
https://gist.github.com/guness/df12d8cc4f595af1395f4a1f5bca5f00
In addition, I think some kind of merging support for the data sources would be a nice to have feature for pagination library.
i had same issue
@yigit Can anyone answer this issue please.
Already had this discussion here:
If you have a collection shared w/ a RecyclerView, that collection CANNOT be modified w/o telling RecyclerView. So all modification & notifications needs to happen on the main thread and in the same call stack.
We could technically create a DataSource API that allows passing such partial changes but then we would need to log all of them and re-deliver to the RV on the main thread. The issue w/ that approach is that, if you have a RecyclerView that is stopped (a.k.a back stack), it will not (should not) receive any updates so PagedList would be keeping this potentially long list of items and re-apply on main thread for each observer (again some observers may have received some events).
This greatly complicates the problem, which is why we go w/ a single list.
Also, none of those optimizations would work w/ sqlite (so yea, that had a part). You can easily create a DataSource that can take a previous list and keep modifications on it, but still return it as a list. In other words, implement that tracking logic in your DataSource which would not need to deal w/ RecyclerView's thread restrictions.
TL;DR: To update a pagedList you have to create a new list, this is how room update data and you have to do the same if you want to update your list.
I try this after delete local data
liveModel?.pageList?.dataSource?.invalidate()
After re-reading the documentation under Consider How Content Updates Work it appears the only way to have realtime updates is via implementing Room with the PagedList: _If you're loading data directly from a Room database updates get pushed to your app's UI automatically._
I'm planning to connect my Firestore queries to Room in order to be able to remove items from my PagedList without having to invalidate the DataSource and reload the entire RecyclerView.
@AdamSHurwitz
If you connect your server data with Room to create PagedList from it, even then when you delete an item from your local DB your DataSource will be invalidated automatically and you will get another version of PagedList from Room in your onChanged()
Am I right?
@abhinav272 This is not the case. When an individual item in Room is added or modified the PagedList updates the corresponding entry rather than refreshing all of the data in the component. Therefore I'm able to update Room with Firestore data when it changes and allow Room + PagedList to handle the updates on individual cells. As long as Room is updated of the change the PagedList will animate and update/add/remove the item modified.
Conversely, if Firestore data was connected directly to the PagedList the entire data set would need to be _invalidated_ / refreshed since the PagedList cannot automatically recognize individual changes from Firestore.
@AdamSHurwitz
Do you get a callback in onChanged() when you update the data in Room??
If so, that means Room has invalidated the DataSource and has provided new one with PagedList containing new data.
When I create a DataSource which is fetching data from either Room or Network, it gives me a callback in onChanged() only once when the PagedList gets created (upon creation of DataSource) and when the pages are loaded it automatically gets displayed on UI..
Make sense?
@abhinav272 - May you specify which onChangedd() method you are referring to?
I don't make Room updates directly. I update Firestore's database. With Firestore when a piece of information changes the Firestore listener will send an update. I listen for that update and inform Room of the change using a LiveData object. This makes it easy to keep both my backend Firestore and front-end Room data synced with minimal work.
@abhinav272
I had the same kind of issue!!!
I am building an app that receives data from network API which gets chached in Room DB. Now if i have to delete an item ,I send a request to delete an item in Remote DB(Network) and if the response is 204 , I delete an item in the Room , So then the Room DB creates a new PagedList and notifies the UI.... Right??
Or else Do we have to implement in any other way???
@Dilip23 yeah, this looks fine.
@abhinav272
It worked !!! Great!!!
@abhinav272 - This is how I am currently implementing it. When I remove an item from Firestore it also updates my Room Db.
@faris-jameel if I understood correctly the question...
If you use the PagedListAdapter, you don't need to call any notifyItemRemoved() or similar.
So, if you delete your item from Room, the recycler adapter will be updated automatically
@AdamSHurwitz @abhinav272 I have also implemented same way. Whenever I update the room DB, UI is notified with the new list and I call submitList() method. Whenever an item is added at last no problem, but if an item added at the top(as I am querying sorted list from DB) my whole list flickers(the whole list loaded again and whole list UI refreshed), same with remove, if I remove the last item its fine and only that item moves/affected but when top item, whole list seems loading again.
@sonalchopra-vvdn
There may be a problem with your DiffUtil.ItemCallback<>
Fyi, whole list is returned Everytime whether you add/remove/update any item at any position, but the PagedListAdapter is smart enough to draw only those items which are new or changed, it skips re-drawing items which are same and this logic is implemented in your DiffUtil.ItemCallback<> overridden methods.
I made a sample using PagedListAdapter https://github.com/roby222/recyclerViewSample
I'm currently work on that because the page size settings doesn't work (I setted 20 items at a time, but I receive all db items!)
@roby222
By default PagedListBuilder has placeholders set to true, so you may receive whole list but after 20 items all items will be null.
If you want you can set placeholders to false.
I would love to see an example of this using anything else then room.
I need a sample that without room too.
need a sample that without room too.
I have created some custom adapter to handle this case(only a part of it actually). Luckily I was supposed to add on top of current list, so following solution only supports adding to top. However you can try to customize it.
https://gist.github.com/guness/df12d8cc4f595af1395f4a1f5bca5f00
In addition, I think some kind of merging support for the data sources would be a nice to have feature for pagination library.
@guness Maybe you want to update the class with PagedListAdapterHelper
We're discussing options to better support granular updates in Paging3, but currently to delete / remove an item you'll need to update the backing db, then invalidate your DataSource / PagingSource. Room handles propagating invalidation for you, but if you're not using Room you'll need to manually call DataSource.invalidate(). Your DataSouce.Factory should then generate a new instance of DataSource to load the updated data.
In Paging3, we also provide APIs to transform a PagingData (new PagedList) as we expose PagingData as a Flow. You can theoretically combine / filter / map / etc, in whatever way your heart desires, but the backing mechanism is still the same. We're looking into ways to provide more granular updates without invalidating the whole list as it's a common use case, but Paging3 is still very early in alpha.
Most helpful comment
I need a sample that without
roomtoo.