🔑
【Android】PagingSource でキーの重複を有効にする
PagingSource
でnextKey
が重複していると、以下のエラーが発生してクラッシュします。
エラーログ
java.lang.IllegalStateException:
The same value, [key], was passed as the nextKey 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.
キーが重複してもクラッシュしないようにするには、ログに書いてあるとおりkeyReuseSupported
をオーバーライドして、true
を返すようにします。
PagingSource
class SamplePagingSource : PagingSource<String, Entity>() {
override fun getRefreshKey(state: PagingState<String, Entity>): String? = null
override suspend fun load(params: LoadParams<String>): LoadResult<String, Entity> {
return try {
...
} catch (e: Throwable) {
LoadResult.Error(e)
}
}
// Add
override val keyReuseSupported: Boolean
get() = true
}
Discussion