I migrated from Paging 2 to Paging 3 library and now I use the PagingSource to load pages of data from the server. But I have trouble understanding the getRefreshKey method that has to be overriden there. I found some code samples how to implement it depending on the key used to fetch subsequent pages but I still don't get it. Based on these samples I wrote the following code:
override fun getRefreshKey(state: PagingState<Pair<String, String>, User>): Pair<String, String>? {
return state.anchorPosition?.let { anchorPosition ->
state.closestPageToPosition(anchorPosition)?.prevKey
}
}
But it doesn't change a thing if I make it to always return null:
override fun getRefreshKey(state: PagingState<Pair<String, String>, User>): Pair<String, String>? = null
So is there a reason why I can't just choose the simplest solution possible? Or is there a use case that I don't see?
So, this would be the difference:
Say you have your working list. A user is scrolling the list and there is some change in the list (the data was updated lets say). Then:
Case when getRefreshKey() returns null always: As your data is updated, there will be a new pair of PagingSource and PagingData, meaning that the paging lib. would need to reload the list. If getRefreshKey() returns null, then it will not know what page was last accessed and will return the user to the start of the list (as the key for position will be decided by the key in load()).
On the other hand, if getRefreshKey() is implemented properly, then: paging lib. will know the last accessed position and the list will not jump to the top. Instead it will show the last accessed page.
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