Forcing Paging Library DataSource refresh

liminal picture liminal · Nov 6, 2018 · Viewed 10.9k times · Source

In my ViewModel, I load data using

private val pagingConfig = PagedList.Config.Builder()
    .setEnablePlaceholders(false)
    .setInitialLoadSizeHint(INITIAL_LOAD_SIZE_HINT)
    .setPageSize(PAGE_SIZE)
    .build()

val notificationList = LivePagedListBuilder<Long, Notification>(dataSourceFactory, pagingConfig).build()

Which works fine. However, when my data changes, LiveData<PagedList<Notification>> does not get notified. Is there anything I can do to trigger LiveData refresh (the ViewModel knows when the change occurs).

Answer

Alexey Denysenko picture Alexey Denysenko · Nov 7, 2018

You can trigger data update using invalidate() method from the DataSource.

When using the Paging Library, it's up to the data layer to notify the other layers of your app when a table or row has become stale. To do so, call invalidate() from the DataSource class that you've chosen for your app.

More information: Notify when data is invalid