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