Arr*_*Cen 2 android-paging android-paging-library android-paging-3
我正在按照此Codelab使用 github API 和本地数据库构建 paging3 应用程序。虽然前 2 个页面加载良好,但当滚动到底部时尝试加载第 3 个页面时,调解器会遇到循环 - 相同的 PagingState 一遍又一遍地传递给 load() 函数。
只是想知道是否有人知道这里可能的根本原因是什么?
一些实施细节:
RemoteMediator:(prevPage 和 currentPage 来自 github API 的分页响应标头并保存到本地数据库。)
// 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)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Observable:我正在使用liveData而不是flowto from 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
}
Run Code Online (Sandbox Code Playgroud)
Dao:只是一个简单的查询
@Query("SELECT * FROM repository_table WHERE login = :ownerLoginName")
fun getRepositoriesOfUser(ownerLoginName: String): PagingSource<Int, Repository>
Run Code Online (Sandbox Code Playgroud)
对于任何感兴趣的人,修复来自 Dao,需要更新查询以对 reponame 进行排序,否则即使有新项目插入到数据库中,查询也会为 PagingSource 返回相同的最后一个页面,从而使 Mediator 感到困惑。
@Query("SELECT * FROM repository_table WHERE login = :ownerLoginName ORDER BY repository_name ASC")
fun getRepositoriesOfUser(ownerLoginName: String): PagingSource<Int, Repository>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1317 次 |
| 最近记录: |