0

In my API, there is no pageSize or unique id for each products is there, but I still implemented Paging 3 library and comparator inside adapter as well with live data for latest changes.

Now the problem is: Suppose total only 10 items are there in my API, and I fetchd it and showing in list. But once I scroll 10 items, again that 10 items are showing, and again...and infinite!!

I suspect 3 mistakes of mine, one because of wrong implementation with paging (next prev key), or 2nd problem is because of comparator and 3rd, I overriden getRefreshKey and passed state.anchorPosition.

Code:

ProductPagingSource

class ProductPagingSource(
    private val productApi: ProductApi,
) : PagingSource<Int, Products>() {

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

        return try {
            val response =
                productApi.getProducts() //Not added load size, position and other params, because in API, there is no data related to that.
            val products = response.products

            LoadResult.Page(
                data = products,
                prevKey = if (position == PRODUCT_STARTING_PAGE_INDEX) null else position - 1,
                nextKey = if (products.isEmpty()) null else position + 1
            )
        } catch (exception: IOException) {
            LoadResult.Error(exception)
        } catch (exception: HttpException) {
            LoadResult.Error(exception)
        }
    }

    override fun getRefreshKey(state: PagingState<Int, Products>): Int? {
        return state.anchorPosition
    }
}

ProductAdapter

class ProductAdapter(context: Context?) :
    PagingDataAdapter<Products, ProductAdapter.ProductViewHolder>(PRODUCT_COMPARATOR) {
    private lateinit var mContext: Context

    init {
        if (context != null) {
            mContext = context
        }
    }

.
.
.

 private val PRODUCT_COMPARATOR = object : DiffUtil.ItemCallback<Products>() {
            override fun areItemsTheSame(oldItem: Products, newItem: Products) =
                oldItem.name == newItem.name

            override fun areContentsTheSame(oldItem: Products, newItem: Products) =
                oldItem == newItem
        }
    }
}

1 Answers1

1

Without the API documentation, I can only answer this in a general manner:

When you tell it to always fetch the same data, it will probably always fetch the same data - this means that our business logic is flawed, because productApi.getProducts() needs to fetch per key (which is often called the "next page token"). In case the API should not to support pagination, you'd have to use a repository and then query the local SQLite with paging support.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Hey, thanks for the quick reply, but in my API there is no per key or like that option provided, And second, there are only 10 data and that data is showing properly and after a moment, when I scroll down more again that 1st to 10 data is shoeing –  Feb 27 '21 at 18:24
  • this is for a demo purpose, and I did by watching a tutorial, in that tutorial all key and things like that is there. I want to do the same, assuming I'll also get similar api. Just understnd that this is my requirement –  Feb 27 '21 at 18:26
  • if there's only 10 items in your list, why are you trying to use pagination here? just load the 10 items _once_ ? as this answer already explained to you, you're calling the same data over and over again, why would that ever change ? – a_local_nobody Feb 27 '21 at 18:44
  • Actually I don't want to, but the thing is I followed one tutorial series (14 videos) and in that he used dagger hilt+ mvvm+ paging + glide n all. So I've also one assignment to do so I following that one again, but the thing is he used pagination and did further videos accroding to that, now If I'll break that sequence, there will be a lot of changes, and I'll stuck somewhere –  Feb 27 '21 at 18:49
  • 1
    that means your question doesn't _actually_ involve pagination at all, right ? you need to go and do research on how to do api calls without the pagination library, because at this point you don't need it. that means, this answer is totally correct, because you are using pagination for no real reason – a_local_nobody Feb 27 '21 at 18:51
  • @a_local_nobody I did as per your suggestion. I removed all paging code as well as it's dependencies. but now facing this: https://stackoverflow.com/questions/66403177/why-does-apply-called-first-before-viewmodel-observer-in-android Please help –  Feb 27 '21 at 20:26