androidrx-javaandroid-paging-3android-paging-library

Issue with Paging3 and Rxjava in MVP


In my project I want to use paging 3 .

before adding paging into my project , I could get the data from server and show into my RecyclerView but after adding paging I faced with this issue :

in my Paging Source class :

class RepoPagingSource @Inject constructor(
private val repository: ApiRepository,
val context: Context) : PagingSource<Int, RepositoryResponse>() {

private lateinit var sharedPref: SharedPref
private lateinit var data : MutableList<RepositoryResponse>
private lateinit var responseCode : String

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, RepositoryResponse> {
    sharedPref = SharedPref(context)
    val responseData = mutableListOf<RepositoryResponse>()

    return try {
        val currentPage = params.key ?: 1
        val response = repository
            .getRepositories("bearer ${sharedPref.accessToken}", currentPage)
            .applyIoScheduler()
            .subscribe { response ->
                responseCode=response.code().toString()
                data = response.body()!!
                Log.d("RepoPagingSource",responseCode)
                Log.d("RepoPagingSource",data.size.toString())
                Log.d("RepoPagingSource",data.toString())
            }


        responseData.addAll(data)
        LoadResult.Page(
            data = responseData,
            prevKey = if (currentPage == 1) null else -1,
            nextKey = currentPage.plus(1)
        )


    } catch (e: Exception) {
        LoadResult.Error(e)
    }
}

override fun getRefreshKey(state: PagingState<Int, RepositoryResponse>): Int? {
    return null
}

}

these log is showed correct data :

Log.d("RepoPagingSource",responseCode)
Log.d("RepoPagingSource",data.size.toString())
Log.d("RepoPagingSource",data.toString())

result of these logs :

RepoPagingSource: 200
RepoPagingSource: 2
RepoPagingSource: [RepositoryResponse(id=5246349....

but my recyclerview is empty and i checked the code in debug mode here : responseData.addAll(data) data is null!

thanks in advance for your help


Solution

  • I have done it like :

    class RepoPagingSource @Inject constructor(
    private val repository: ApiRepository,
    val context: Context ) : RxPagingSource<Int, RepositoryResponse>() {
    
    private lateinit var sharedPref: SharedPref
    
    override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, RepositoryResponse>> {
        sharedPref = SharedPref(context)
    
        var nextPageNumber = params.key
        if (nextPageNumber == null) {
            nextPageNumber = 1
        }
    
        return repository.getRepositories("bearer ${sharedPref.accessToken}", nextPageNumber)
            .subscribeOn(Schedulers.io())
            .map { response: Response<MutableList<RepositoryResponse>> -> response.body()?.let { toLoadResult(it, nextPageNumber) } }
            .onErrorReturn { LoadResult.Error(it) }
    }
    
    private fun toLoadResult(
        response: MutableList<RepositoryResponse>,
        position:Int
    ): LoadResult<Int, RepositoryResponse> {
    
        return LoadResult.Page(
            response,
            null,
            position + 1,
            COUNT_UNDEFINED,
            COUNT_UNDEFINED
        )
    
    }
    
    override fun getRefreshKey(state: PagingState<Int, RepositoryResponse>): Int? {
        return null
    }}
    

    in its work for me ,also i have changed my ver of library Rx into rxjava2