1

I would like to add some item in current paging data which already contains items. And shown in the list adapter.

I have tried to insertHeaderItem. But, when adding second item. Header is getting replaced by second item. I want to keep all item in the list. and update that list every time when i add new item.

              paingData
                    .insertHeaderItem(item = sampleViewEvents.comment)
                    .let {
                        getCommentList.value = it
                    }

I doesn't want to call an API to refresh the whole list. It can leads a performance issue.

Thanks in advance

Vaibhav K
  • 43
  • 2
  • 12

1 Answers1

1

The correct way to add / modify items with Paging is to update the backing dataset and .invalidate(), refreshing the list. This maintains a single source of truth and allows Paging to be aware of the changes you've made.

For performance, you can cache your data into a middle DB / in-memory layer, and load items from network via RemoteMediator. If you do this (which you should if you are concerned about performance), the reload becomes quite trivial. See: https://developer.android.com/topic/libraries/architecture/paging/v3-network-db

If you are interested in updating a specific page, you can follow this FR: https://issuetracker.google.com/160232968.

To be clear, you MUST go through the invalidate loop to update the items loaded by Paging. Not doing it this way is currently completely unsupported. It is the only way to make Paging aware of the changes you've made.

dlam
  • 3,547
  • 17
  • 20
  • Is there any way without invalidate(). I don't want to call paging data again, it should just call old observer and add new item in list. – Vaibhav K Sep 22 '21 at 09:50
  • No there is no supported method without invalidating, you must make paging aware of these changes until we add support in https://issuetracker.google.com/160232968. There's a lot of questions for simply inserting an item. For example, in how it plays with transformations, page dropping due to maxSize, caching, preserving across multiple collectors (e.g., config change), fast scrolling / jumping, etc. It's much more complicated than simply notifying the adapter, and to make sure that Paging handles everything correctly for you, you absolutely need to make Paging aware of your changes. – dlam Sep 22 '21 at 18:01
  • I would also add that I totally respect that this is a highly requested feature, but practically, it is just not that expensive to reload a single page from disk / in-memory cache, so we've been prioritizing other fixes / features. – dlam Sep 22 '21 at 18:02
  • @dlam Simple data insertion is so tricky. What kind of separator is discussed? The separator is the icing on the cake, and the data insertion is just a function. – gaohomway Oct 07 '21 at 09:08
  • @weifans Part of the issue here is that the correct way to insert data is to update your backing dataset. It's very important that any reload will keep your changes. – dlam Oct 07 '21 at 16:19
  • @dlam I have read a lot of answers, but I still can’t update and insert. Regarding the use of `.invalidate()`, can you update the answer? Use the code to ensure that people who care about this problem can use it in a simple and clear way instead of searching for answers everywhere. – gaohomway Oct 08 '21 at 01:37
  • It's hard for me to give you sample code since it depends on your `PagingSource`. If you want to ask a new question which shows your `PagingSource`, I'm happy to help you implement something. When you invalidate, `Flow` emits a new `PagingData`. When you call `.submitData()`, you start loading, which asks `PagingSource` to reload about the user's current scroll position. E.g., the common case of `PagingSource` loading from a DB, you would just insert into the DB and then call `.invalidate()`, which would then reload from DB, picking up the new item. – dlam Oct 08 '21 at 01:48
  • @dlam Thank you, I will continue to study about `.invalidate()`, now I have encountered a new problem, the page will automatically scroll to the top, I use Compose, when there are multiple `items`, there will be problems, I submitted a problem, please trouble you take a look https://stackoverflow.com/questions/69489581/jetpack-compose-paging-3-auto-scroll-to-the-top – gaohomway Oct 08 '21 at 01:58