android - 当我不使用 ContigouslyPagedList 时,ContigouslyPagedList onPageError

标签 android androidx android-jetpack android-paging-library

我实现了 PositionalDataSource,它定义了 loadInitialloadRange。分页工作正常,但在某些边界条件下(可能与退出屏幕时正在加载下一页有关)应用程序崩溃,并显示 androidx.paging.ContigouslyPagedList$1.onPageError (ContigouslyPagedList.java:153)。然而每https://developer.android.com/reference/android/arch/paging/PositionalDataSource我的源不连续?

在 ContigeousPagedList 的 onPageError 下发生崩溃,与“todo”一致:

public void onPageError(@PageResult.ResultType int resultType,
        @NonNull Throwable error, boolean retryable) {

   LoadState errorState = retryable ? LoadState.RETRYABLE_ERROR : LoadState.ERROR;

    if (resultType == PageResult.PREPEND) {
        mLoadStateManager.setState(LoadType.START, errorState, error);
    } else if (resultType == PageResult.APPEND) {
        mLoadStateManager.setState(LoadType.END, errorState, error);
    } else {
        // TODO: pass init signal through to *previous* list
        throw new IllegalStateException("TODO");
    }
}

我的配置没有使用占位符,并且我没有将总计数传递到 LoadInitialCallbackonResult 中。分页库版本为2.1.1

最佳答案

至少目前(版本 2.1.1)没有好的、干净的解决方案。

ContigeousPagedList 不支持初始化错误处理。 您必须实现并行流来加载状态和自定义重试。

参见示例in the official architecture components samples library 。 Yigit Boyar 通过返回自定义实现了几个流 Listing来自存储库的对象。

data class Listing<T>(
        // the LiveData of paged lists for the UI to observe
        val pagedList: LiveData<PagedList<T>>,
        // represents the network request status to show to the user
        val networkState: LiveData<NetworkState>,
        // represents the refresh status to show to the user. Separate from networkState, this
        // value is importantly only when refresh is requested.
        val refreshState: LiveData<NetworkState>,
        // refreshes the whole data and fetches it from scratch.
        val refresh: () -> Unit,
        // retries any failed requests.
        val retry: () -> Unit)

查看数据源here的实现

更新

经过多次不同的尝试,我最终从架构的核心部分中删除了 Jetpack,并将其仅保留在 View 模型中,现在对我来说看起来干净多了。您可以阅读here

关于android - 当我不使用 ContigouslyPagedList 时,ContigouslyPagedList onPageError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59973742/

相关文章:

android - 防止 LaunchedEffect 在配置更改时重新运行

java - Android - 使用快照遍历firebase数据库

android - Android Studio 中的 "package android.support.v7.app does not exist"错误

android - Google Play 预发布报告崩溃 : java. lang.NoSuchMethodError

android - Android 中哪一种更好的设计方法 - 多个 Activity/Fragments

android - android中的MediaSessionCompat和MediaSession有什么区别?为什么有 androidx.media2 androidx.media 都存在?

android - 如何用 Gradle 3.0 替换 apt()

android - 如果方向改变,相机不会覆盖旧图像

android - 在 Activity 中保持屏幕开启 - 不适用于 FLAG_KEEP_SCREEN_ON

AndroidX 在按钮上设置背景颜色没有效果