2

我正在使用分页 3 库(3.0.0-alpha03),我的本地缓存是 Room(160,000 行)。

当我使用滚动条并拖动到 RecyclerView(GridLayoutManager) 中的特定位置时,如何加快从本地缓存项加载“ClosestToCurrentPosition”?

假设我直接把滚动条拖到150000的位置,“Flow”贴图功能会从0到150000触发,速度极慢,效率不高

/**
 * Returns a [PagingData] containing the result of applying the given [transform] to each
 * element, as it is loaded.
 */
@CheckResult
@JvmSynthetic
fun <T : Any, R : Any> PagingData<T>.map(
    transform: suspend (T) -> R
): PagingData<R> = transform { it.map(transform) }

我的预期是这样的:当我使用滚动条并拖动到 150,000 时,加载行为将加载大约 154,500~150,500

PagingConfig(pageSize = 500, initialLoadSize = 1000, prefetchDistance = 50, enablePlaceholders = true)

有人可以帮我看看这个问题吗?或者 Dustin Lam 你能帮我吗?谢谢

编辑:我尝试将数据源包装为跳跃源,但发生了一些奇怪的事情。这是我的跳跃来源

class XnMediaFileJumpingPositionPagingSource(

        private val roomPagingSource: PagingSource<Int, SimpleXnMediaFile>

) : PagingSource<Int, SimpleXnMediaFile>() {

    override val jumpingSupported = true

    val roomInvalidateCallback = roomPagingSource.registerInvalidatedCallback {
        Timber.e("[jumping] PagingSource invalidate()")
        invalidate()
        
        // cannot compile
//        unregisterInvalidatedCallback {
//            invalidateCallback
//        }
//        roomPagingSource.unregisterInvalidatedCallback { 
//            roomInvalidateCallback
//        }
    }

    val invalidateCallback = registerInvalidatedCallback {
        Timber.e("[jumping] RoomPagingSource invalidate()")
        roomPagingSource.invalidate()

        // cannot compile
//        unregisterInvalidatedCallback {
//            invalidateCallback
//        }
//        roomPagingSource.unregisterInvalidatedCallback {
//            this.roomInvalidateCallback
//        }
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, SimpleXnMediaFile> {
        Timber.e("[jumping] Load key %s loadsize %s placeholder %s ", params.key, params.loadSize, params.placeholdersEnabled)
        return roomPagingSource.load(params)
    }

    @ExperimentalPagingApi
    override fun getRefreshKey(state: PagingState<Int, SimpleXnMediaFile>): Int? {
        Timber.e("[jumping] getRefreshKey %s", state.anchorPosition)
        return state.anchorPosition
    }

当我将滚动条拖到列表的一半时params.key不断变化(可能是位置 80,000 ,我的数据库数据集行数量是 160,000 如上所述),最后是应用程序 ANR

这是日志

2020-07-27 13:06:22.590 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key null loadsize 800 placeholder true 
2020-07-27 13:06:29.107 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 85749
2020-07-27 13:06:29.108 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:06:29.109 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:06:29.127 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 85749 loadsize 800 placeholder true 
2020-07-27 13:06:30.920 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 171060
2020-07-27 13:06:30.920 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:06:30.920 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:06:30.926 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 171060 loadsize 800 placeholder true 
2020-07-27 13:06:32.808 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 85869
2020-07-27 13:06:32.809 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:06:32.809 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:06:32.816 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 85869 loadsize 800 placeholder true 
2020-07-27 13:06:34.572 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 85200 loadsize 400 placeholder true 
2020-07-27 13:06:34.724 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 163199
2020-07-27 13:06:34.725 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:06:34.725 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:06:34.729 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 163199 loadsize 800 placeholder true 
2020-07-27 13:07:06.274 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 162400 loadsize 400 placeholder true 
2020-07-27 13:07:06.443 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 85199
2020-07-27 13:07:06.443 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:07:06.443 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:07:06.447 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 85199 loadsize 800 placeholder true 
2020-07-27 13:07:09.094 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 84400 loadsize 400 placeholder true 
2020-07-27 13:07:09.236 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 162399
2020-07-27 13:07:09.237 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:07:09.238 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:07:09.249 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 162399 loadsize 800 placeholder true 
2020-07-27 13:07:40.931 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 161600 loadsize 400 placeholder true 
2020-07-27 13:07:41.158 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 84419
2020-07-27 13:07:41.158 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:07:41.159 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:07:41.162 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 84419 loadsize 800 placeholder true 
2020-07-27 13:07:43.690 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 84000 loadsize 400 placeholder true 
2020-07-27 13:07:43.837 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 161599
2020-07-27 13:07:43.837 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:07:43.838 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:07:43.840 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 161599 loadsize 800 placeholder true 
2020-07-27 13:08:15.157 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 160800 loadsize 400 placeholder true 
2020-07-27 13:08:15.338 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 84019
2020-07-27 13:08:15.339 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:08:15.339 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:08:15.342 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 84019 loadsize 800 placeholder true 
2020-07-27 13:08:17.980 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 83600 loadsize 400 placeholder true 
2020-07-27 13:08:18.123 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 160799
2020-07-27 13:08:18.124 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:08:18.125 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:08:18.136 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 160799 loadsize 800 placeholder true 
2020-07-27 13:08:49.178 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 160000 loadsize 400 placeholder true 
2020-07-27 13:08:49.359 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 83619
2020-07-27 13:08:49.360 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:08:49.360 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:08:49.364 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 83619 loadsize 800 placeholder true 
2020-07-27 13:08:51.997 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 83200 loadsize 400 placeholder true 
2020-07-27 13:08:52.148 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 159999
2020-07-27 13:08:52.148 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:08:52.149 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:08:52.152 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 159999 loadsize 800 placeholder true 
2020-07-27 13:09:22.903 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 159200 loadsize 400 placeholder true 
2020-07-27 13:09:23.090 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 83219
2020-07-27 13:09:23.090 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:09:23.090 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:09:23.094 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 83219 loadsize 800 placeholder true 
2020-07-27 13:09:25.713 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 82800 loadsize 400 placeholder true 
2020-07-27 13:09:25.856 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 159199
2020-07-27 13:09:25.857 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:09:25.857 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:09:25.860 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 159199 loadsize 800 placeholder true 
2020-07-27 13:09:56.329 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 158400 loadsize 400 placeholder true 
2020-07-27 13:09:56.494 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 82819
2020-07-27 13:09:56.494 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:09:56.494 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:09:56.497 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 82819 loadsize 800 placeholder true 
2020-07-27 13:09:59.132 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 82400 loadsize 400 placeholder true 
2020-07-27 13:09:59.273 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 158399
2020-07-27 13:09:59.273 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:09:59.273 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:09:59.276 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 158399 loadsize 800 placeholder true 
2020-07-27 13:10:29.469 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 157600 loadsize 400 placeholder true 
2020-07-27 13:10:29.629 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 82419
2020-07-27 13:10:29.629 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:10:29.629 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:10:29.633 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 82419 loadsize 800 placeholder true 
2020-07-27 13:10:32.251 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 82000 loadsize 400 placeholder true 
2020-07-27 13:10:32.397 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 157599
2020-07-27 13:10:32.397 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:10:32.397 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:10:32.401 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 157599 loadsize 800 placeholder true 
2020-07-27 13:11:02.096 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 156800 loadsize 400 placeholder true 
2020-07-27 13:11:02.250 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 81999
2020-07-27 13:11:02.250 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:11:02.250 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:11:02.253 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 81999 loadsize 800 placeholder true 
2020-07-27 13:11:04.901 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 81200 loadsize 400 placeholder true 
2020-07-27 13:11:05.043 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] getRefreshKey 156799
2020-07-27 13:11:05.043 12810-12810/com.app.fun E/JumpingPagingSource$invalidateCallback: [jumping] RoomPagingSource invalidate()
2020-07-27 13:11:05.044 12810-12810/com.app.fun E/JumpingPagingSource$roomInvalidateCallback: [jumping] PagingSource invalidate()
2020-07-27 13:11:05.046 12810-12810/com.app.fun E/JumpingPagingSource: [jumping] Load key 156799 loadsize 800 placeholder true

和分页配置

return Pager(
        config = PagingConfig(
                pageSize = 400,
                initialLoadSize = 800,
                prefetchDistance = 160,
                enablePlaceholders = true,
                maxSize = (pageSize + prefetchDistance) * 5,
                jumpThreshold = 4000
        ),
        remoteMediator = XnMediaFileRemoteMediator(xnbay, contract, db),
        pagingSourceFactory = pagingSourceFactory
).flow

我错过了什么 ?

4

1 回答 1

2

您可以设置一个PagingConfig.jumpThreshold值(通常是页面大小的倍数,具体取决于每个页面加载所需的时间)。这将在快速滚动的情况下触发 invalidate + REFRESH 并将用户当前的滚动位置传递给PagingSource.getRefreshKey(state)via state.anchorPosition

为了选择加入,您必须重写PagingSource.jumpingSupported以返回true并实施PagingSource.getRefreshKey(state)以返回正确的 REFRESH 加载键,这将允许用户从其当前视图位置重新开始加载。

编辑:如果您使用的是 Room,从 2.3.0-alpha02 开始,PagingSource它生成的实现仍然使用兼容性路径,因此您需要用自己的实现包装它以启用跳转。

像这样的东西:

class JumpingPositionPagingSource(
  val roomPagingSource<Int, Model>
) : PagingSource<Int, Model>() {
    val roomInvalidateCallback = roomPagingSource.registerInvalidatedCallback {
      invalidate()
      unregisterInvalidatedCallback(invalidateCallback)
      roomPagingSource.unregisterInvalidatedCallback(roomInvalidateCallback)
    }
    val invalidateCallback = registerInvalidatedCallback {
      roomPagingSource.invalidate()
      unregisterInvalidatedCallback(invalidateCallback)
      roomPagingSource.unregisterInvalidatedCallback(roomInvalidateCallback)
    }

  override suspend fun load(loadParams: LoadParams<Int>) {
    return roomPagingSource.load(loadParams)
  }

  override val jumpingSupported = true

  override fun getRefreshKey(state: PagingState<Int, Model>): Int? {
    return state?.anchorPosition
  }
}
于 2020-07-25T19:30:35.843 回答