RemoteMediator and Caching
Combine network and database paging.
Offline-First Paging
A plain network PagingSource works, but if the connection drops the list is empty. For a robust app you want the database as the single source of truth and the network filling it in the background.
That is exactly what RemoteMediator enables: page from the local database, and fetch more from the network when the database runs out.
The Architecture
With RemoteMediator the data flows in one direction:
- The UI pages from a Room
PagingSource - When Room nears its end,
RemoteMediatorfetches the next network page - The network result is written into Room
- Room emits the new rows, and the UI updates
The user always sees cached data instantly, even offline.
// Network ---> RemoteMediator ---> Room (source of truth) ---> UI
// UI never reads the network directlyAll lessons in this course
- Why Paging
- PagingSource and Pager
- Paging in Compose Lists
- RemoteMediator and Caching