Caching is not working in Android Paging 3
Solution 1:
If you're able to share your code or how you reached that conclusion I could probably help pinpoint the problem a bit better, but the codelab does load data from Room on the branch: step13-19_network_and_database
There are two components here:
PagingSource
: Provided by Room by declaring a @Query
with a PagingSource
return type, will create a PagingSource
that loads from Room. This function is called in the pagingSourceFactory
lambda in Pager
which expects a new instance each call.
RemoteMediator
: load()
called on boundary conditions where the local cache is out of data, this will fetch from network and store in the Room db, which automatically propagates updates to PagingSource
implementation generated by Room.
One other issue you might be seeing could be related to loadStateListener/Flow
, essentially the codelab shows an error state by checking for CombinedLoadStates.refresh
, but this always defers to the RemoteMediator
's load state when available and if you want to show the locally cached data, even when RemoteMediator
errors out, you'll need to disable hiding of the list in that case.
Note that you can access individual LoadState
with CombinedLoadStates.source
or CombinedLoadStates.mediator
.
Hopefully this is enough to help you, but it's hard to guess your issue without some more concrete example / information about what you're seeing.
Edit: While the above are still good things to check for, it looks like there's an underlying issue with the library that I'm chasing down here: https://android-review.googlesource.com/c/platform/frameworks/support/+/1341068
Edit2: This is fixed now and will be released with alpha02.