androidpaginationaws-amplifyandroid-pagingandroid-paging-library

PagingSource from paging library 3 with callback as result


Currently, I am trying to migrate to the new paging 3 library of Android, but if I see it right, I can't :(

I'm using AWS Amplify as my backend data source and want to include a query into the new load function of the PaginSource class from the paging library.

override suspend fun load(params: LoadParams<String>): LoadResult<String, Car> {
          val query = ListCarsQuery.builder().limit(params.loadSize).build()

          appSyncClient.query(query)
             .responseFetcher(AppSyncResponseFetchers.CACHE_AND_NETWORK)
             .enqueue(
                object : GraphQLCall.Callback<ListCarsQuery.Data>() {
                    override fun onResponse(response: Response<ListCarsQuery.Data>) {
                        val result = CarTransformer.toModels(response)
                        // Here is my actual result list
                    }

                    override fun onFailure(e: ApolloException) {
                        TODO("Not yet implemented")
                    }
                }
        )

          //How can I add my result list here ? 
          return LoadResult.Page(
             data = listOf(),
             prevKey = null,
             nextKey = ""
            )

since the method enqueues gives me a void back I don't know how I could wait on it or trigger a callback like in paging library 2. In paging 2 I had the option to call the callback.onResult(result.data, result.nextLink) method in the enqueue().onResponse function without having to give anything back.

Is there a way to achieve it or should I stick with paging 2?


Solution

  • Paging3 doesn't offer a callback API (yet), so you would need to wrap it into an RxJava Single, a Guava ListenableFuture, or into a suspending Kotlin coroutine.

    Rx version of PagingSource is available in paging-rxjava2/3 artifacts, and Guava's is in paging-guava.

    In terms of the actual conversion, it would be a lot to list all the possibilities, but for example there are Kotlin Coroutine builders that allow you to wrap and await xallbacks in a suspending context. See suspendCancellableCoroutine as an example, you basically get a Continuation object you can call resume(result) on.