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.
DataSource#invalidate()
method - it will create a newDataSource
though... – Stalingradinvalidate()
nothing happens. it just stops further data loading when scroll RecyclerView – Hardininvalidate
thenLivePagedListProvider#createDataSource
should be called – Stalingradinvalidate()
performs its action only once, the docs say:"Signal the data source to stop loading, and notify its callback. If invalidate has already been called, this method does nothing."
- i have used it and it works just fine - if in your case it was not called at all and now it is called billion times then it seems that your code is somehow broken – StalingradcreateDataSource
. that is why it's looped. now list is refreshed the list from blank state. now it would be nice to find the way to not drop previous data and just merge old dataset with new dataset – HardinDataSource
/DataSourceFactory
in my project, I was getting a lot of weird bugs. Instead of that I added a query method in my DAO interface that returns aDataSource.Factory<Int, Something>
and I use it directly from there. – Nerine