我有一个连续调用loadAfter的PageKeyedDataSource,并且所有项目都多次添加到Recyclerview中。从API方面来看,空lastEvaluatedKey
意味着给我第一页,这对于为什么它不断调用以获取第一页是合理的,但是A。如果没有更多数据要获取,它就应该停止(aka params.key == null
?和B。适配器中的COMPARATOR
是否不应该禁止多次添加相同的项目,我还缺少什么?
PageKeyedDataSource.kt
class ReservationsPageKeyedDataSource(private val retryExecutor: Executor) : PageKeyedDataSource<String, Reservation?>() {
private var retry: (() -> Any)? = null
val initialLoad = MutableLiveData<PagingNetworkState>()
fun retryAllFailed() {
val prevRetry = retry
retry = null
prevRetry?.let {
retryExecutor.execute {
it.invoke()
}
}
}
override fun loadInitial(
params: LoadInitialParams<String>,
callback: LoadInitialCallback<String, Reservation?>
) {
val request = Api.reservationsService.getReservations(dateType = RERVATIONS_DATE_TYPE.future, last = null)
initialLoad.postValue(PagingNetworkState.LOADING)
// triggered by a refresh, execute in sync
try {
val response = request.execute()
val originalData = response.body()?.result?.reservations
val data = mutableListOf<Reservation>()
// some data munipulation
retry = null
initialLoad.postValue(PagingNetworkState.LOADED)
callback.onResult(
data.toList(),
null,
response.body()?.result?.lastEvaluatedKey.toString()
)
} catch (ioException: IOException) {
retry = {
loadInitial(params, callback)
}
val error = PagingNetworkState.error(ioException.message ?: "unknown error")
initialLoad.postValue(error)
}
}
override fun loadBefore(
params: LoadParams<String>,
callback: LoadCallback<String, Reservation?>
) {
// no-op
}
override fun loadAfter(
params: LoadParams<String>,
callback: LoadCallback<String, Reservation?>
) {
// I tried adding an if statement here to check if the params.key is null or not but that didn't help
Api.reservationsService.getReservations(dateType = RERVATIONS_DATE_TYPE.future, last = params.key)
.enqueue(object : Callback<ReservationListResponse> {
override fun onFailure(call: Call<ReservationListResponse>, t: Throwable) {
retry = { loadAfter(params, callback) }
}
override fun onResponse(
call: Call<ReservationListResponse>,
response: Response<ReservationListResponse>
) {
if (response.isSuccessful) {
val data = response.body()?.result?.reservations
retry = null
callback.onResult(
data.orEmpty(),
response.body()?.result?.lastEvaluatedKey.toString()
)
} else {
retry = { loadAfter(params, callback) }
}
}
})
}
}
PagedListAdapter中的比较器:
companion object {
val COMPARATOR = object : DiffUtil.ItemCallback<Reservation>() {
override fun areContentsTheSame(oldItem: Reservation, newItem: Reservation): Boolean =
oldItem == newItem
override fun areItemsTheSame(oldItem: Reservation, newItem: Reservation): Boolean =
oldItem.id == newItem.id
}
}
最佳答案
该代码看起来大部分都很好,但是您似乎想通过toString将响应的下一个标记部分转换为字符串,并将其用作键,这似乎很奇怪。
代替PageKeyedDataSource<String, Reservation?>
,请尝试PageKeyedDataSource<KeyType, Reservation>
(不知道该 key 的类型来自上面的代码)。
然后,您可以直接从API获取下一个 token ,然后将其传递到API的last
参数中,而无需对其进行修改。
您还应该使用不可为空的Reservation
-分页库希望加载的项目为非空,因为保留了空值以表示占位符:https://developer.android.com/reference/androidx/paging/DataSource#implementing-a-datasource