I have implementation of PositionalDataSource that defines loadInitial
and loadRange
. Paging works fine however under some boundary condition (likely related with loading next page in progress while exiting the screen) app crashes with androidx.paging.ContiguousPagedList$1.onPageError (ContiguousPagedList.java:153)
. However a per https://developer.android.com/reference/android/arch/paging/PositionalDataSource my source is not contiguous?
Crash occurs under ContiguousPagedList's onPageError, line with "todo":
public void onPageError(@PageResult.ResultType int resultType,
@NonNull Throwable error, boolean retryable) {
LoadState errorState = retryable ? LoadState.RETRYABLE_ERROR : LoadState.ERROR;
if (resultType == PageResult.PREPEND) {
mLoadStateManager.setState(LoadType.START, errorState, error);
} else if (resultType == PageResult.APPEND) {
mLoadStateManager.setState(LoadType.END, errorState, error);
} else {
// TODO: pass init signal through to *previous* list
throw new IllegalStateException("TODO");
}
}
My configuration is not using placeholders, and I'm not passing total count into onResult
of LoadInitialCallback
. Version of paging library is 2.1.1
I experienced the same behaviour because I called callback.onError(e)
when an exception happened in loadInitial
and loadAfter
methods of my PageKeyedDataSource
implementation. In order to prevent the app from crashing I removed the callback.onError(e)
line. I know this is not a solution, but at least my app is not crashing randomly.
Example code (Kotlin+coroutines incoming)
override fun loadInitial(params: LoadInitialParams<Long>, callback: LoadInitialCallback<Long, Beer>) {
scope.launch {
try {
val response =
BackendService.client.getBeers(
name = searchQueryNullable,
page = 1,
pageSize = params.requestedLoadSize
)
callback.onResult(response, 1, 2)
} catch (e: Exception) {
Log.e(tag, "Error loadInitial", e)
// callback.onError(e) <-- this is the line that generates the crash: comment it
}
}
}