I am working on Paging 3 library and facing a issue which is like in my PagingSource class my load(params: LoadParams) returns the value before I get the response from API. I have added callback for the API response but still I am facing the same problem.
Any hack on how to solve this?
class FeedDatasource(var application: FeedActivity) :
PagingSource<Int, FeedModel.Success_data>(), APICallback {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, FeedModel.Success_data> {
return try {
// Start refresh at page 1 if undefined.
nextPage = params.key ?: 1
getFeedsData(params)
LoadResult.Page(
data = feedModel.success_data!!,
prevKey = if (nextPage == 1) null else nextPage - 1,
nextKey = nextPage + 1
)
} catch (e: Exception) {
LoadResult.Error(e)
}
}
}
}
// APICallback override method
override fun success(response: MutableLiveData<JsonElement>) {
response.observe(application, { jsonElement ->
feedModel = Gson().fromJson(jsonElement, FeedModel::class.java)
})
}
fun getFeedsData(params: LoadParams<Int>) {
val nextPage = params.key ?: 1
if (nextPage <= feedModel.total_page!!) {
UserRepository.callAPI(
getJsonObject(nextPage),
AppConstants.APIMarks.FEED_LIST,
AppConstants.HTTPMethod.HTTP_POST, this
)
}
}
interface APICallback {
fun success(response: MutableLiveData<JsonElement>)
fun failure()
}