Append data after load in Remote Mediator
还没有人认领这个 Issue。
评估
调研方向
首先跟踪 Pager 经过 MapPagingSource.load 和 PokemonApiRemoteMediator.load 的流程,然后将记录的序列与提交给 adapter 的 PagingData 进行比较。检查 APPEND 期间内存中的 map 如何变化,以及哪个事件应该使新加载的数据可见。完成的标准是:追加的结果会出现在 adapter 中,而不依赖无关的 workaround。
由索引模型根据 Issue 内容生成。
描述
I decided to try Pager without the Room database. I use HashMap as the simplest database.
First I implemented PagingSource.
class MapPagingSource(
private val map: () -> Map<Int, List<Result>>
) : PagingSource<Int, Result>() {
override fun getRefreshKey(state: PagingState<Int, Result>): Int? {
println("TAG PagingSource anchorPosition ${state.anchorPosition}")
val anchor = state.anchorPosition ?: return null
val closest = state.closestPageToPosition(anchor) ?: return null
val page = closest.prevKey?.plus(1) ?: closest.nextKey?.minus(1)
return page
}
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Result> {
val map = map()
val position = (params.key ?: 0)
println("TAG PagingSource position = $position, size = ${map.size}")
return LoadResult.Page(
data = map[position] ?: emptyList(),
prevKey = if (position == 0) null else position - 1,
nextKey = if (position + 1 < map.size) position + 1 else null
)
}
}
After RemoteMediator
@OptIn(ExperimentalPagingApi::class)
class PokemonApiRemoteMediator(
private val map: () -> MutableMap<Int, List<Result>>,
private val append: () -> Unit
) : RemoteMediator<Int, Result>() {
private val ktor = Ktor()
override suspend fun initialize(): InitializeAction {
println("TAG RemoteMediator initialize")
return InitializeAction.LAUNCH_INITIAL_REFRESH
}
override suspend fun load(loadType: LoadType, state: PagingState<Int, Result>): MediatorResult {
val map = map()
println("TAG RemoteMediator $loadType, ${map.size}")
return try {
val latestPage = when (loadType) {
LoadType.REFRESH -> 0
LoadType.PREPEND -> return MediatorResult.Success(endOfPaginationReached = true)
LoadType.APPEND -> map.size
}
val subList = runCatching {
withContext(Dispatchers.IO) {
ktor.getData(state.config.pageSize, latestPage * state.config.pageSize)
}
}.getOrNull()
val data =
subList?.results ?: return MediatorResult.Success(endOfPaginationReached = true)
if (data.isEmpty()) return MediatorResult.Success(endOfPaginationReached = true)
if (loadType == LoadType.REFRESH) map.clear()
map[latestPage] = data
println("TAG RemoteMediator size ${map.size}")
if (loadType == LoadType.APPEND) append()
MediatorResult.Success(false)
} catch (e: Throwable) {
println("TAG RemoteMediator $e")
MediatorResult.Error(e)
}
}
}
And created a Pager in ViewModel
private val data: MutableMap<Int, List<Result>> = HashMap()
private var latestSource: MapPagingSource? = null
@OptIn(ExperimentalPagingApi::class)
private val _pager = Pager(
config = PagingConfig(10, enablePlaceholders = false),
remoteMediator = PokemonApiRemoteMediator(map = { data }) { },
pagingSourceFactory = {
println("TAG create source")
MapPagingSource { data }.also {
latestSource = it
} }
val adapter = Adapter()
init {
_pager.flow
.onEach {
println("TAG PagingData $it")
adapter.submitData(it)
}.launchIn(viewModelScope)
}
As I can see, the PagingData was created and sent to the adapter.
Then the PagingSource did not find the data and started the RemoteMediator.
After loading, the work stopped and the PagingSource did not start loading the data.
Of course, I can call "invalidate". But this will recreate the PagingSource, and will not add the data as if it were just making requests to the network from the PagingSource.
- 主要语言
- Kotlin
- 星标
- 509
- 派生
- 268
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
android/codelab-android-paging 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 62/100
android/codelab-android-paging#236 · 3 个 reaction ·
-
难度 3/5 1-2 天 新手友好度 35/100
-
难度 2/5 1-3 小时 新手友好度 35/100
android/codelab-android-paging#243 · 1 条评论 · 1 个 reaction ·
-
难度 2/5 1-2 天 新手友好度 35/100
android/codelab-android-paging#241 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 15/100
查看 android/codelab-android-paging 的全部 Issue
相似的 Issue
-
OAI-PMH
难度 2/5 1-3 小时 新手友好度 78/100
-
难度 2/5 1-3 小时 新手友好度 90/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 86/100
TeamAmaze/AmazeFileManager#4731 ·
-
Bug Needs review
难度 2/5 1-3 小时 新手友好度 70/100
breezy-weather/breezy-weather#2978 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 92/100
AAswordman/Operit#1292 ·
维护者通常 1 天内回复