Jim*_*era 5 android android-paging android-paging-library android-paging-3
我一直在努力研究如何解决我的RemoteMediator's问题APPEND LoadType。
在空的 Room DB 上,LoadType流程如下:REFRESH -> PREPEND -> APPEND (remoteKeys = null, endOfPaginationReached = true)
实体和远程键表至少有 10 行,LoadType流程如下:REFRESH -> PREPEND -> APPEND (remoteKeys = prev=null, next=2, endOfPaginationReached = false)
显然,我的问题出在新安装的设备上(带有空的 Room DB),用户不会看到超过 10 个项目,因为APPEND'sstate.lastItemOrNull()正在返回null。
到目前为止,这是我的代码:
private suspend fun getRemoteKeysForLastItem(state: PagingState<Int, MovieCache>): MovieRemoteKeys? {
return state.lastItemOrNull()?.let { movie ->
appDatabase.withTransaction {
appDatabase.remoteKeysDao().remoteKeysByImdbId(movie.imdbId)
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于我的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)
}
}
Run Code Online (Sandbox Code Playgroud)
让我的应用程序无法向下滚动至少一次!
最后,这就是我如何通过依赖数据库上的远程密钥来解决这个问题PagingState:
LoadType.APPEND -> {
// val key = getRemoteKeysForLastItem(state) // Doesn't work. state returns NULL even Room has data for both remote keys and entity.
val key = appDatabase.withTransaction {
remoteKeysDao.allKeys().lastOrNull() // Workaround
}
key?.nextKey ?: return MediatorResult.Success(true)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
249 次 |
| 最近记录: |