【发布时间】:2021-04-08 15:27:11
【问题描述】:
我有一个这样的PagingSource:
class NotificationPagingSource(
private val tayehAPI: TayehAPI,
private val token: String,
) : PagingSource<Int, Notification>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Notification> {
val nextPageNumber = params.key ?: STARTING_PAGE_INDEX
return try {
val response = tayehAPI.getNotifications(
tokenString = token,
page = nextPageNumber,
perPage = params.loadSize
)
val notifications = response.objects
LoadResult.Page(
data = notifications,
prevKey = if (nextPageNumber == STARTING_PAGE_INDEX) null else nextPageNumber - 1,
nextKey = if (notifications.isEmpty()) null else nextPageNumber + 1
)
} catch (e: IOException) {
LoadResult.Error(e)
} catch (e: HttpException) {
LoadResult.Error(e)
}
}
override fun getRefreshKey(state: PagingState<Int, Notification>): Int? {
return state.anchorPosition?.let { anchorPosition ->
val anchorPage = state.closestPageToPosition(anchorPosition)
anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
}
}
}
我想处理 HttpExceptions 之类的 403 或 404。我该如何实现?
【问题讨论】: