Event Handler
pagingFLow.apply {
when {
loadState.refresh is LoadState.Loading -> {}
loadState.refresh is LoadState.NotLoading -> {}
loadState.append is LoadState.Loading -> {
item { CircularProgressIndicator() }
}
loadState.refresh is LoadState.Error -> {
val e = pagingFLow.loadState.append as LoadState.Error
Log.i("ERROR", e.toString())
}
loadState.append is LoadState.Error -> {
val e = pagingFLow.loadState.append as LoadState.Error
Log.i("ERROR", e.toString())
}
}
}
Here val e = pagingFLow.loadState.append as LoadState.Error
throws androidx.paging.LoadState$NotLoading cannot be cast to androidx.paging.LoadState$Error
This works fine with DataSource without DB but When you add RemoteMediator it throws this error.
As philip dhukov alluded, you are making a cast which is not guaranteed to succeed
loadState.append
can be anything in the following:
pagingFLow.apply {
when {
...
loadState.refresh is LoadState.Error -> {
val e = pagingFLow.loadState.append as LoadState.Error
Log.i("ERROR", e.toString())
}
...
}
}
You actually probably meant:
pagingFLow.apply {
when {
...
loadState.append is LoadState.Error -> {
Log.i("ERROR", loadState.append.toString())
}
...
}
}
Note: You dont need to explicitly cast after type check due to smart casting so you can omit the cast entirely.
Also, it is generally not safe to access a member and assume the value won't change in case of race conditions, so you should use loadState.member
instead of pagingFLow.loadState.member
in case this is called concurrently. Although it may be fine for your case it will also prevent kotlin from smart casting after type check