skip to content

As Android lead, should a wine catalogue page through a RemoteMediator into Room or straight from the network with a PagingSource, and what does each choice commit the team to?

level: principalimportance: should knowfreq 26%

answer

  1. offline and process death
  2. who owns the item when it changes
  3. an experimental API in production
  4. schema, remote keys and invalidation
  5. cachedIn is memory only

basics

~20 s

Use a RemoteMediator into Room when the list must work offline, survive process death or reflect local edits; page straight from the network when results are throwaway. The mediator costs an experimental API, a schema with remote keys and harder tests.

solid answer

~40 s

A network `PagingSource` is the cheaper default: one class, a `getRefreshKey()`, and `cachedIn(viewModelScope)` to survive rotation, but the cache lives in memory, so process death or a relaunch refetches and offline users see nothing. A `RemoteMediator` makes the database the source of truth: cached wines page in offline, `initialize()` can skip a network refresh while the cache is fresh, and a local edit such as a favourite flag updates the list because writing the row invalidates the DAO's `PagingSource`. The price is an `@ExperimentalPagingApi` dependency that may change source-incompatibly, tables and remote keys to migrate, transaction discipline, more `LoadState` combinations in the UI and database-backed tests. I would use the mediator for primary browse feeds that need offline and the plain source for ephemeral results such as search, and write that rule down.

go deeper

for a junior

Recall that a RemoteMediator caches pages in a database for offline use, while a network PagingSource keeps pages only in memory.

for a middle

Explain how each option survives rotation versus process death and how a local item edit reaches the list in each.

for a senior

List the concrete costs of the mediator, from remote keys and transactions to split LoadStates and database-backed tests, and the bugs each one invites.

for a principal

Decide per feed, write the rule down with its reasons, and keep the repository contract as a Flow of PagingData so the choice can be reversed without touching screens.

## The decision Paging 3 gives two supported shapes for a list backed by a remote API: - **Network only**: a hand-written `PagingSource` calls the API, the `Pager` streams it, and `cachedIn(viewModelScope)` keeps loaded pages for the ViewModel's lifetime. - **Network into a database**: a `RemoteMediator` writes API pages into a database, and the list reads from the database's `PagingSource`. Neither is right everywhere, and the call is usually made per feed. The lead's job is to make it deliberately and record why. ## What each one buys and costs | Concern | Network `PagingSource` | `RemoteMediator` + database | |---|---|---| | Offline browsing | none: nothing to show without a response | cached pages load from the database | | Process death, relaunch | refetches from the first page | cached rows come back immediately | | Local edits to an item | need a `refresh()` or an overlay | updating the row invalidates and reloads the list | | Freshness control | every new stream hits the network | `initialize()` chooses `SKIP_INITIAL_REFRESH` or `LAUNCH_INITIAL_REFRESH` | | API stability | stable APIs only | `@ExperimentalPagingApi` opt-in | | Code to own | one class and its refresh key | entities, DAOs, a remote keys table, migrations, transactions | | UI states | `refresh`, `append`, `prepend` | the same, split into `source` and `mediator` | | Tests | `TestPager` or `asSnapshot` with a fake API | an in-memory database plus the mediator's paths | ## Arguments for the mediator 1. **Offline is a product requirement.** A catalogue people browse in a cellar with poor reception must show the last wines it saw. 2. **Items change locally.** A favourite toggle written to the wine's row flows to every screen reading that table, because Room invalidates the list's `PagingSource` and Paging reloads around the anchor. A network-only list has to call `refresh()` or combine an in-memory overlay after `cachedIn`. 3. **Several screens share the data.** A detail screen reading the same table stays consistent with the list. 4. **Cold starts matter.** Returning users see cached rows at once, while the network refresh runs behind them. ## Arguments for network only 1. **The results are ephemeral.** Search results, filtered views and one-off lists gain little from persistence, and a table per query becomes cache management. 2. **The team avoids experimental APIs** in a module or library it publishes. The `@ExperimentalPagingApi` marker says the API may change source-incompatibly in a later version. 3. **Fewer moving parts.** No schema migration, no remote keys and none of the race the Android guide describes, where queued `APPEND` or `PREPEND` requests carry an out-of-date `PagingState` after a `REFRESH`. 4. **Memory is bounded anyway.** `PagingConfig.maxSize` can drop old pages from a long in-memory list; it defaults to `MAX_SIZE_UNBOUNDED`, so a team that relies on it has to set it. ## Questions that settle it - Does the screen have to show something with no network? - Can the user change an item from this list or another screen? - Must the list survive process death without refetching? - Is someone accountable for the schema, migrations and the remote-keys contract? - Is the team comfortable shipping an opt-in API, and who tracks it across Paging releases? ## What the choice does to the UI The screen code barely changes, which is what makes the decision reversible. Both shapes expose `Flow<PagingData<Wine>>` and both present through a `PagingDataAdapter` or the Compose integration. The differences show up in loading states: with a mediator, `CombinedLoadStates.source` reports database loads and `CombinedLoadStates.mediator` network loads, so the screen can keep cached rows visible while a network refresh runs and show a non-blocking error when only the network failed. A network-only screen has one source of truth for errors, and a failed refresh with nothing cached means an empty screen with a retry. ## A rule worth writing down A reasonable team default is: - primary browse feeds that need offline use a `RemoteMediator` into the app database, with `initialize()` honouring a cache timeout; - search and other ephemeral lists use a network `PagingSource` with `cachedIn(viewModelScope)`; - both expose `Flow<PagingData>` from the repository, so a screen never knows which one it is reading and a feed can switch later without UI changes. What this decision does not cover belongs elsewhere: how the server cursor works, and how page windows and refresh merging are designed independently of any library, are system design questions, not Paging API choices.

  • A network-only list must show a favourite toggle immediately. How do you do it without a database?
    Keep favourite ids in a `StateFlow` in the ViewModel and `combine` it with the paged flow **after** `cachedIn(viewModelScope)`, mapping each `PagingData` with `pagingData.map { it.copy(isFavourite = it.id in ids) }`. The cache keeps the loaded pages and each emission is a fresh `PagingData`, so nothing reloads, but the overlay is lost on process death unless the ids are persisted.
  • What would make you move a feed from network-only to a RemoteMediator later?
    A product requirement for offline browsing, item edits that must show across screens, or cold-start complaints that refetching causes. If the repository already exposes `Flow<PagingData<Wine>>`, the switch stays inside the data layer: add the tables, the mediator and the opt-in, and swap the `pagingSourceFactory` for the DAO's source.

  • cachedIn keeps the loaded pages across process death
  • RemoteMediator is the only supported way to use Paging with a network API
  • Every paged list in the app should go through the database for consistency
  • Network-only lists cannot reflect a local edit without a database
  • The opt-in annotation on RemoteMediator is a formality with no upgrade risk