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状态即可。

相关内容

热门资讯

透视模拟器!wepoker辅助... 透视模拟器!wepoker辅助器安装包,aapoker破解侠是真的吗,安装教程(有挂规律)-哔哩哔哩...
透视app!poker红龙辅助... 透视app!poker红龙辅助,hhpoker德州透视,第三方教程(存在有挂)-哔哩哔哩hhpoke...
透视总结!werplan免费挂... 透视总结!werplan免费挂下载,wepoker的辅助器,透视教程(竟然有挂)-哔哩哔哩运werp...
透视存在!wejoker内置辅... 透视存在!wejoker内置辅助,hhpoker有作弊的吗,2025新版教程(有挂辅助)-哔哩哔哩1...
透视美元局!aapoker插件... 透视美元局!aapoker插件下载,佛手大菠萝辅助,高科技教程(存在有挂)-哔哩哔哩1、下载好佛手大...
透视智能ai!wepokerp... 透视智能ai!wepokerplus透视脚本免费,wepoker轻量版透视系统,新2025版(详细教...
透视玄学!wepoker辅助器... 透视玄学!wepoker辅助器最新版本更新内容,拱趴大菠萝十三水作弊,专业教程(竟然有挂)-哔哩哔哩...
透视透视!wpk私人局辅助是真... 透视透视!wpk私人局辅助是真的吗,德普之星私人局辅助免费,曝光教程(有挂方针)-哔哩哔哩1、不需要...
透视中牌率!wepoker怎么... 透视中牌率!wepoker怎么下载游戏,aapoker怎么控制牌,2025新版技巧(有挂实锤)-哔哩...
透视模拟器!wepoker亲友... 透视模拟器!wepoker亲友圈有用吗,wpk可以作弊吗,靠谱教程(有挂助手)-哔哩哔哩1、任何we...