【发布时间】:2021-06-23 02:04:40
【问题描述】:
我一直在尝试解决我与RemoteMediator 的APPEND LoadType 的问题。
在空的 Room DB 上,LoadType 的流动方式如下:
REFRESH -> PREPEND -> APPEND (remoteKeys = null, endOfPaginationReached = true)
实体和远程键表至少有 10 行,LoadType 的流动方式如下:
REFRESH -> PREPEND -> APPEND (remoteKeys = prev=null, next=2, endOfPaginationReached = false)
显然,我的问题是在新安装的设备上(房间数据库为空),用户不会看到超过 10 个项目,因为APPEND 的state.lastItemOrNull() 正在返回null。
到目前为止,这是我的代码:
private suspend fun getRemoteKeysForLastItem(state: PagingState<Int, MovieCache>): MovieRemoteKeys? {
return state.lastItemOrNull()?.let { movie ->
appDatabase.withTransaction {
appDatabase.remoteKeysDao().remoteKeysByImdbId(movie.imdbId)
}
}
}
对于我的load() 函数:
val loadKey = when (loadType) {
LoadType.REFRESH -> {
val key = getRemoteKeysClosestToCurrentPosition(state)
Timber.d("REFRESH key: $key, output: ${key?.nextKey?.minus(1)}")
key?.nextKey?.minus(1) ?: 1
}
LoadType.PREPEND -> {
Timber.d("PREPEND key requested")
return MediatorResult.Success(true)
}
LoadType.APPEND -> {
val key = getRemoteKeysForLastItem(state)
Timber.d("APPEND key: $key")
appDatabase.withTransaction {
val size = movieDao.movies().size
val remoteSize = remoteKeysDao.allKeys().size
Timber.d("APPEND DB size: $size, remote: $remoteSize")
}
key?.nextKey ?: return MediatorResult.Success(true)
}
}
这是一个示例 logcat,显示 APPEND 是 null
让我的应用至少无法向下滚动一次!
【问题讨论】:
标签: android android-paging android-paging-library android-paging-3