kotlin - PageKeyedDataSource loadAfter 连续调用

标签 kotlin android-architecture-components android-paging

我有一个连续调用loadAfter的PageKeyedDataSource,所有的项目都多次添加到Recyclerview中。从 API 方面来看,一个空 lastEvaluatedKey 意味着给我第一页,这对于为什么它一直调用以获取第一页但 A 有点道理。如果没有更多数据要获取,它不应该停止(又名 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
        }
    }

最佳答案

另一个可能的原因是,如果您在 ScrollView 内部使用 Recycleview,loadAfter 将被无限调用,直到数据完成。
Recycleview 会自动处理滚动,所以不要在 scrollView 中使用它。

关于kotlin - PageKeyedDataSource loadAfter 连续调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58735434/

相关文章:

配置更改后,Android 导航组件重置为开始目的地

android - 如何将 PagedListAdapter 与多个 LiveData 一起使用

spring-boot - 如何在 Spring WebFlux 中记录请求和响应主体

java - 分页编译问题: Not sure how to convert a Cursor to this method's return type

android - 在 Android Studio 中使用 safeArgs 导致错误,我无法构建项目

android - 如何在 Architecture Components ViewModel 中刷新数据

android - java/kotlin 模块中的 Jetpack Paging 3

android - 如何在Android分页库中为列表、过滤器和搜索维护相同的数据源

testing - 使用多个 `--tests`

swing - 如何在 JavaFX 应用程序中显示 OpenCV 网络摄像头捕获帧