gpt4 book ai didi

Android Paging 3 : LoadType. APPEND 返回空远程键

转载 作者:行者123 更新时间:2023-12-04 11:39:52 27 4
gpt4 key购买 nike

我一直在尝试解决我的问题 RemoteMediatorAPPEND LoadType .
在空的 Room DB 上,LoadType流量:
REFRESH -> PREPEND -> APPEND (remoteKeys = null, endOfPaginationReached = true)
实体和远程键表至少有 10 行,这就是 LoadType 的方式流量:
REFRESH -> PREPEND -> APPEND (remoteKeys = prev=null, next=2, endOfPaginationReached = false)
显然,我的问题是在新安装的设备上(房间数据库为空),用户不会看到超过 10 个项目,因为 APPENDstate.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,显示 APPENDnull enter image description here
让我的应用程序至少无法向下滚动一次!
编辑:这是我保存远程 key 的方法:
enter image description here

最佳答案

最后,这就是我通过依赖数据库上的远程键而不是 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)
}

关于Android Paging 3 : LoadType. APPEND 返回空远程键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66813622/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com