Paging Library: How to reload portion of data on demand?

deviant picture deviant · Oct 28, 2017 · Viewed 8.2k times · Source

I use Paging Library to paginate my data set. What I'm trying to do is to refresh the RecyclerView after data in my database has been changed.

I have this LiveData:

val listItems: LiveData<PagedList<Snapshot>> = object : LivePagedListProvider<Long, Snapshot>() {
        override fun createDataSource() = SnapshotsDataSource()
    }.create(null, PagedList.Config.Builder()
        .setPageSize(PAGE_SIZE)
        .setInitialLoadSizeHint(PAGE_SIZE)
        .setEnablePlaceholders(false)
        .build()
    )

And the DataSource:

class SnapshotsDataSource : KeyedDataSource<Long, Snapshot>(), KodeinGlobalAware {

    val db: FirebaseDb = instance()

    override fun getKey(item: Snapshot): Long = item.timestamp

    override fun loadInitial(pageSize: Int): List<Snapshot> {
        val result = db.getSnapshotsTail(pageSize)
        return result
    }

    override fun loadAfter(key: Long, pageSize: Int): List<Snapshot> {
        val result = db.getSnapshotsTail(key, pageSize)
        return result.subList(1, result.size)
    }

    override fun loadBefore(key: Long, pageSize: Int): List<Snapshot> {
        return emptyList()
    }

}

The Adapter is straight forward, so i omit it here.

I've tried to do this when database is modified:

fun reload(position) {
    listItems.value!!.loadAround(position)
}

but it didn't help.

Answer

Pavel Filippov picture Pavel Filippov · Jun 12, 2018

try to call listItems.value!!.datasource.invalidate() not directly DataSource#invalidate()