У меня есть базовый класс для смещения страниц, поэтому он имеет значения 0–40, 40–60, 60–80 и так далее. И это работает, но когда я нахожусь в середине коллекции и хочу обновить данные либо с помощью validate(), либо с помощью адаптера.refresh(), происходит сбой со следующим сообщением:
< р>
Код: Выделить всё
java.lang.IllegalStateException: The same value, 40, was passed as the prevKey in two sequential Pages loaded from a PagingSource. Re-using load keys in PagingSource is often an error, and must be explicitly enabled by overriding PagingSource.keyReuseSupported.Пример:
prevKey null, nextKey: 40 -> prevKey 40, nextKey: 60 -> prevKey 60,
nextKey: 80
Invalidate()
prevKey 40, nextKey: 80 - > prevKey 40, nextKey: 60 // Это тот случай,
когда происходит сбой без флага keyReuseSupported.
И как я хочу назад к началу он застрял на prevKey 40, nextKey: 60
Код: Выделить всё
abstract class OffsetPagingSource(
private val coroutineScope: CoroutineScope
) : PagingSource() {
abstract suspend fun queryFactory(
size: Int,
after: Int,
itemCount: Boolean
): PagingResult?
abstract suspend fun subscriptionFactory(): Flow?
init {
initSubscription()
}
private fun initSubscription() {
coroutineScope.launch {
try {
subscriptionFactory()?.collect {
invalidate()
}
} catch (e: Throwable) {}
}
}
override suspend fun load(params: LoadParams): LoadResult {
val size = params.loadSize
val after = params.key ?: 0
return try {
val result = queryFactory(size, after, true) ?: return LoadResult.Page(emptyList(), null, null)
val totalCount = result.pageInfo.itemCount
val nextCount = after + size
val prevKey = if (after == 0) null else after
val nextKey = if (nextCount < totalCount) nextCount else null
LoadResult.Page(result.edges.mapNotNull { it.node }, prevKey = prevKey, nextKey)
} catch (e: Exception) {
LoadResult.Error(e)
}
}
override fun getRefreshKey(state: PagingState): Int? {
return state.anchorPosition?.let { anchorPosition ->
val anchorPage = state.closestPageToPosition(anchorPosition)
anchorPage?.prevKey
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/712 ... the-middle
Мобильная версия