AndroidPaging3:LoadType.APPEND返回null的远程键值
创始人
2024-10-09 11:32:00
0

在使用Paging 3进行分页时,我们可能会遇到在LoadType.APPEND时返回null的远程键值的问题。此时,我们需要检查以下几点:

  1. RemoteMediator对象中的load函数是否正确返回远程键值。在该函数中,我们需要获取最后一条加载数据的键值,并将其返回。

示例代码:

override suspend fun load(loadType: LoadType, state: PagingState): MediatorResult { val page = when (loadType) { LoadType.REFRESH -> { // This is triggered by a swipe-to-refresh. // Don't use remote key since refresh will clear all data. null } LoadType.PREPEND -> { // This is triggered when the user scrolls to the top of the list. // We should use the first available remote key, which is closest to the position. val remoteKeys = getClosestRemoteKeys(state) remoteKeys?.prevKey ?: return MediatorResult.Success(endOfPaginationReached = !remoteKeys.isNullOrEmpty()) } LoadType.APPEND -> { // This is triggered when the user scrolls to the end of the list. // We should use the last available remote key, which is closest to the position. val remoteKeys = getClosestRemoteKeys(state) remoteKeys?.nextKey ?: return MediatorResult.Success(endOfPaginationReached = !remoteKeys.isNullOrEmpty()) } }

// ...

val apiResponse = service.getData(page ?: 1)

val items = apiResponse.data
val endOfPaginationReached = items.isEmpty()

// ...

val keys = items.map {
    RemoteKeys(repoId = it.id, prevKey = page, nextKey = page)
}
db.remoteKeysDao().insertAll(keys)

return MediatorResult.Success(endOfPaginationReached = endOfPaginationReached)

}

  1. 在刷新(LoadType.REFRESH)时,不要使用远程键值(remote key)。因为刷新将清除所有数据,打破了该键与数据的对应关系。

  2. 在LoadType.PREPEND时,使用最接近位置的第一个可用远程键值(closest available remote key)。如果没有可用的键值,则返回success状态即可。

  3. 在LoadType.APPEND时,使用最接近位置的最后一个可用远程键值(closest available remote key)。如果没有可用的键值,则返回success状态即可。

相关内容

热门资讯

第四分钟带你普及!hhpoke... 第四分钟带你普及!hhpoker作弊实战视频(辅助挂)一贯是有挂的(今日头条)-哔哩哔哩;亲,有的,...
一分钟了解"九九联盟... 一分钟了解"九九联盟辅助神器"开挂(透视)辅助工具(一贯确实是有挂的)-哔哩哔哩;九九联盟辅助神器免...
四分钟带你科普!圣游辅助器(辅... 四分钟带你科普!圣游辅助器(辅助挂)果然确实有挂(今日头条)-哔哩哔哩;一、圣游辅助器有挂的是的,亲...
玩家攻略"乐乐围棋入... 【福星临门,好运相随】;玩家攻略"乐乐围棋入门破解"开挂(透视)辅助神器(其实是真的有挂的)-哔哩哔...
玩家实测"八闽状元郎... 玩家实测"八闽状元郎app破解版辅助"开挂(透视)辅助软件(原来确实有挂的)-哔哩哔哩;大家肯定在之...
8分钟带你开挂!天酷辅助器(辅... 8分钟带你开挂!天酷辅助器(辅助挂)原来真的有挂(有挂教学)-哔哩哔哩;天酷辅助器最新软件透视开挂直...
发现玩家"柳州八一字... 发现玩家"柳州八一字牌辅助"开挂(透视)辅助平台(一贯确实是有挂的)-哔哩哔哩;一、柳州八一字牌辅助...
1分钟带你介绍!三哥玩摆头挂(... 1分钟带你介绍!三哥玩摆头挂(辅助挂)一直有挂(有挂存在)-哔哩哔哩;三哥玩摆头挂是一项三哥玩摆头挂...
透视计算"决战卡五星... 透视计算"决战卡五星辅助修改器"开挂(透视)辅助工具(果然确实是有挂的)-哔哩哔哩;决战卡五星辅助修...
第一分钟带你解说!财神十三章如... 第一分钟带你解说!财神十三章如何提高运气(辅助挂)果然是有挂的(有挂技术)-哔哩哔哩;大家肯定在之前...