Can't understand code of getRefreshKey in Android paging3 codelab

January 19, 2022, at 02:10 AM
// GitHub page API is 1 based:
private const val GITHUB_STARTING_PAGE_INDEX = 1
class GithubPagingSource(
        private val service: GithubService,
        private val query: String
) : PagingSource<Int, Repo>() {
    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Repo> {
        val position = params.key ?: GITHUB_STARTING_PAGE_INDEX
        val apiQuery = query + IN_QUALIFIER
        return try {
            val response = service.searchRepos(apiQuery, position, params.loadSize)
            val repos = response.items
            val nextKey = if (repos.isEmpty()) {
            } else {
                // initial load size = 3 * NETWORK_PAGE_SIZE
                // ensure we're not requesting duplicating items, at the 2nd request
                position + (params.loadSize / NETWORK_PAGE_SIZE)
                    data = repos,
                    prevKey = if (position == GITHUB_STARTING_PAGE_INDEX) null else position - 1,
                    nextKey = nextKey
        } catch (exception: IOException) {
            return LoadResult.Error(exception)
        } catch (exception: HttpException) {
            return LoadResult.Error(exception)
    // The refresh key is used for subsequent refresh calls to PagingSource.load after the initial load
    override fun getRefreshKey(state: PagingState<Int, Repo>): Int? {
        // We need to get the previous key (or next key if previous is null) of the page
        // that was closest to the most recently accessed index.
        // Anchor position is the most recently accessed index
        return state.anchorPosition?.let { anchorPosition ->
                ?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1)

This is the code of getRefreshKey function of the paging3 codelab.

I thought that it's just okay to return state.anchorPosition. But why is this returning closestPagetToPosition's previous key plus 1?? This is the link of the paging 3 code lab.

Rent Charter Buses Company
Regular expression help - comma delimited string

Regular expression help - comma delimited string

I don't write many regular expressions so I'm going to need some help on the one

For the code given what will be stored in ArrayList? What are shallow pointers in Java?

For the code given what will be stored in ArrayList? What are shallow pointers in Java?

Suppose we have three Bear objects: momma, poppa, and babyWe create a "bears" ArrayList and add pointers to the three bears to the ArrayList

Change Default Download Location in Php

Change Default Download Location in Php

I want to change default download location in php Using header function I cant' find a parameter for that This is the definition of header

Base 62 conversion

Base 62 conversion

How would you convert an integer to base 62 (like hexadecimal, but with these digits: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')