gpt4 book ai didi

android-paging - Jetpack Paging3 RemoteMediator 在 #load 附加上返回相同的 PagingSatate

转载 作者:行者123 更新时间:2023-12-03 08:29:25 24 4
gpt4 key购买 nike

我正在关注这个codelab使用 github API 和本地数据库构建 paging3 应用程序。虽然前 2 个页面加载良好,但在滚动到底部时尝试加载第 3 个页面时,调解器会遇到循环 - 相同的 PagingState 会一遍又一遍地传递给 load() 函数。

只是想知道是否有人知道这里可能的根本原因是什么?

一些实现细节:

RemoteMediator:(prevPage 和 currentPage 来自 github API 的 pagination response header 并保存到本地数据库。)

// RepositoryMediator
override suspend fun load(
loadType: LoadType,
state: PagingState<Int, Repository>
): MediatorResult {
return when (loadType) {
LoadType.REFRESH -> {
fireRequestForPage(1, true /*clear DB*/)
return Success(endOfPaginationReached = false)
}

LoadType.APPEND -> {
// !!!!!!! kept getting the same state when APPEND is triggered, resulting in same currentPage and nextPage
// get currentPage, nextPage from state.lastItemOrNull
if(currentPage < nextPage) {
fireRequestForPage(nextPage)
Success(endOfPaginationReached = false)
} else {
return Success(endOfPaginationReached = true)
}
LoadType.PREPEND -> {
// get currentPage, prevPage from state.firstItemOrNull
if(currentPage > prevPage) {
fireRequestForPage(prevPage)
Success(endOfPaginationReached = false)
} else {
return Success(endOfPaginationReached = true)
}
}
}
}

Observable:我使用 liveData 而不是 flow 来自 Pager:

fun searchRepositoryWithUserId(userLoginName: String): LiveData<PagingData<Repository>> {
// need to create a new Pager each time because the search query is different
return Pager(
config = PagingConfig(pageSize = PAGE_SIZE, enablePlaceholders = false),
remoteMediator = RepositoryMediator()
) {
repoDao().getRepositoriesOfUser(userLoginName)
}.liveData
}

Dao:只是一个简单的查询

@Query("SELECT * FROM repository_table WHERE login = :ownerLoginName")
fun getRepositoriesOfUser(ownerLoginName: String): PagingSource<Int, Repository>

最佳答案

对于任何感兴趣的人,修复来自 Dao,需要更新查询以对 reponame 进行排序,否则查询将返回 PagingSource 的相同最后一页,即使有新项目插入到 DB 中,也会使 Mediator 感到困惑。

@Query("SELECT * FROM repository_table WHERE login = :ownerLoginName ORDER BY repository_name ASC")
fun getRepositoriesOfUser(ownerLoginName: String): PagingSource<Int, Repository>

关于android-paging - Jetpack Paging3 RemoteMediator 在 #load 附加上返回相同的 PagingSatate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65593284/

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