Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 211
deviant Avatar asked Oct 28 '17 10:10

deviant


People also ask

What is jetpack Paging?

The Paging Library helps you load and display small chunks of data at a time. Loading partial data on demand reduces usage of network bandwidth and system resources.

How does Paging help with sharing of libraries?

The Paging library helps you load and display pages of data from a larger dataset from local storage or over network. This approach allows your app to use both network bandwidth and system resources more efficiently.


2 Answers

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

like image 130
Pavel Filippov Avatar answered Oct 21 '22 08:10

Pavel Filippov


It's not possible. You can invalidate the whole list only: datasource.invalidate().

like image 23
Malachiasz Avatar answered Oct 21 '22 08:10

Malachiasz