Hi guys, I am using pagination library for fetch t...
# android-architecture
k
Hi guys, I am using pagination library for fetch the data from the API call, and got to know that my API calls are made 3 times, with page number: 1 , 2 , 3 With Page Number 1 -> I got success response - which is correct behaviour. But does anyone know why page number 2 and 3 triggers (which is failure)?
Copy code
private const val STARTING_PAGE_INDEX = 1

class AbsencePagingDataSource(
    private val apiProvider: ApiProvider,
    private val absenceDataMapper: AbsenceDataMapper,
    private val workspaceId: String
) :
    PagingSource<Int, AbsenceData>() {
    lateinit var response: ListApiResponse<WorkspaceAbsenceResponse>

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, AbsenceData> {
        val position = params.key ?: STARTING_PAGE_INDEX

        val request = Endpoint.GetWorkspaceAbsence(
            workspaceId = workspaceId,
            Queries = listOf(
                Queries.Paginate(position, 50),   -------> SEE URL BELOW
            )
        )
        return try {
            apiProvider.api.performRequest<ListApiResponse<WorkspaceAbsenceResponse>>(request).map { listApiResponse ->
                response = listApiResponse
            }
            val endOfPaginationReached = response.data.isEmpty()

            LoadResult.Page(
                data = absenceDataMapper.mapToDomain(response),
                prevKey = if (position == STARTING_PAGE_INDEX) null else position - 1,
                nextKey = if (endOfPaginationReached) null else position + 1
            )
        } catch (exception: IOException) {
            LoadResult.Error(exception)
        }
    }

    override fun getRefreshKey(state: PagingState<Int, AbsenceData>): Int? {
        return state.anchorPosition?.let { anchorPosition ->
            state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1)
                ?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1)
        }
    }
}

URL/request looks like -> 
---------------------------
1. url=<https://PATH/api/v3/workspaces/120/absences?page=1&items=50> ----> 200
2. url=<https://PATH/api/v3/workspaces/120/absences?page=2&items=50> ----> 422
3. url=<https://PATH/api/v3/workspaces/120/absences?page=3&items=50> ----> 422
s
default initialLoadSize is 3x pagesize value (you can override it in PagingConfig - but it is done so user does not see loading that soon and paging works smooth)
not sure about the failure, but it is not Paging lib fault I guess
I think your query may be wrong tbh
k
yeah agree it is not fault from Paging but I don't understand why api triggers 3 times? This should not happen even with the success or failure
s
initialLoadSize is x3 of your inital items, so 3*50, api tries to load 150 items
about failure, not sure, maybe library is programmed that way
k
prefetch is x3 of your inital items means where?
I don't understand
s
sorry, actual property name is initialLoadSize
Copy code
Pager(
            config = PagingConfig(
                pageSize = ITEMS_PER_PAGE,
                initialLoadSize = // overridable, by default 3x pagesize
            ),
k
Ohh Okay got it but then what should be the value for the initialLoadSize then? Because if I don't pass anything it will call multiple apis 1 with sucess and other with failuer
Copy code
return Pager(config = PagingConfig(pageSize = 50, prefetchDistance = 0),
            pagingSourceFactory = { AbsencePagingDataSource(apiProvider, workspaceMapper.absenceDataMapper, workspaceId) }).flow
I am using like this and it will call 1 time api with success only 🙂 But I am not sure if I have multiple data then is this works fine or not
s
I would leave it as is, because it is most likely not the reason of your issue, but you can try to make it the same value as pageSize, so you can test behavior with loading new pages lazily and see if something changes
Copy code
prefetchDistance = 0
is not a good choice, see property documentation how it works
422 error is probably a issue with your query
k
Sure thanks because it is not working in my case. It calls only 1st api and it don't increase the page number.
s
try your second request (second page) alone and see if it works
k
No it's not working right now. It never call 2nd page. I minimise the size of pages. 1Page , 20 size --> getting success 2nd Page, 20 size --> API is not triggered
s
Copy code
url=<https://PATH/api/v3/workspaces/120/absences?page=2&items=50> ----> 422
try this in postman/make a retrofit call like this and see if it works. if it doesn’t, this is not paging fault and you can return to whatever works
k
yeah it is not working because I get the data with my 1st api which is page=1&items=50 but my question is why API trigger 3 times then? I update the below code
Copy code
return Pager(config = PagingConfig(pageSize = 1, prefetchDistance = 0),
            pagingSourceFactory = { AbsencePagingDataSource(apiProvider, workspaceMapper.absenceDataMapper, workspaceId) }).flow
and now if I call the API with page=1&items=20 it works but it never call page=2&items=20 (THIS URL NOT TRIGGERED) But using postman , the URL give me the result of page=2 &items=20
Hi Ignore above message... It is working now 🙂
Thank you for the help and quick guidance
s
glad to help
🙏 1
307 Views