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.
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.
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.
try to call listItems.value!!.datasource.invalidate()
not directly DataSource#invalidate()
It's not possible. You can invalidate the whole list only: datasource.invalidate().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With