In a mobile workout app where both the view model and the repository decide whether cached data is fresh, history charts stay stale after a sync; where should that decision live?
answer
- two deciders, two copies
- observe, don't snapshot
- the screen asks, the repository decides
- pull-to-refresh: a request, not a policy
- the expiry values are caching design
basics
~20 sIn the repository. It owns the store and the server, so it alone knows what is stored and when it was fetched; screens observe its data and may ask for a forced refresh, but never judge freshness themselves.
solid answer
~50 sThe stale chart comes from two layers each holding a copy and a rule: the history view model cached last week's data with its own "loaded recently, skip reload" check, so when sync rewrote the store, nothing told it to read again. Move the decision into the repository: it serves from the store, records when each dataset was last fetched and decides whether to refresh in the background. The view model observes the repository's stream instead of taking a one-shot snapshot, so any write to the store, whether from a refresh, a sync or a set logged elsewhere, reaches every screen. The screen can still pass a request such as "force a refresh" on pull to refresh, but the repository applies the rule. How long data stays valid and what invalidates it is a caching-policy question; the point here is that exactly one layer owns it.
go deeper
Recall that one layer, the repository, decides whether stored data is fresh enough, and that screens just show what it provides.
Explain the difference between observing a stream and taking a one-shot snapshot, and why the snapshot misses writes made by sync or by other screens.
Diagnose the stale chart to its two causes, a private copy and a second rule, and lay out the fix: delete both, observe the repository, keep forced refresh as a request, deduplicate fetches.
Make it a team rule that freshness decisions live only in repositories, and review for view models that cache or time their own reloads, since each one reintroduces this bug class.
## The symptom and its two causes A workout app's history screen shows progress charts. After a background sync brings in last night's sets, the charts still show last week. Logging out and in "fixes" it. Tracing it usually finds two separate defects that together make the bug: 1. **A second copy.** The history view model loaded the history once, as a one-shot read, and kept the list in its own field. Later writes to the store never reach that field. 2. **A second rule.** The view model also decides freshness: "if I loaded within the last ten minutes, skip reloading." The repository has its own, different rule. Each layer is right by its own logic, and the screen shows whichever copy the view model's rule picked. The fix is not a better timer. It is putting the decision, and the data, in one place. ## Why the repository is the right owner The **cached-versus-fresh decision** answers "serve what is stored, or fetch first, or serve and fetch in the background?" It needs facts only the data layer has: - what is in the local store for this query, and when it was last fetched; - whether a fetch for the same data is already in flight; - whether there are local changes the server has not seen yet; - whether the device currently has a connection. A view model knows none of these without reaching into the data layer, and a screen knows even less. Owning the decision in the repository also makes it **consistent**: the plan screen and the history screen get the same answer for the same data, and a test of the repository with a fake clock and fake sources covers every screen at once. ## Candidate owners compared | Candidate owner | What it knows | What goes wrong if it decides | |---|---|---| | The screen | That it is visible | Refetches on every appearance, or never; every screen invents its own rule | | The view model | When it last asked | Keeps a private copy; misses writes from sync and other screens | | A use case | The business rule it runs | Duplicates data-layer bookkeeping, and stateless use cases have nowhere to keep it | | The repository | Store contents, fetch times, in-flight work, local changes | Nothing structural: one rule, one copy, one place to test | ## What the presentation layer may still do Moving the decision does not make screens passive: - A **pull to refresh** sends a request with a "force" flag. The repository still performs the fetch, writes the store and reports status; the screen sees the result through the same stream. - A screen may state its **tolerance**, for example that the plan screen accepts an hour-old plan while a live leaderboard does not. The repository applies that tolerance with its own bookkeeping. - A screen can **display** freshness, such as "updated 5 min ago", from a timestamp the repository exposes. The difference is between asking and deciding. ## Fixing the history screen 1. Delete the view model's private history list and its "loaded recently" check. 2. Have the view model subscribe to the repository's observable history and build chart state from each new value. 3. Move the staleness check into the repository, keyed by query, using the last-fetched timestamp it records when it writes the store. 4. Make the repository's background refresh deduplicate: two screens asking at once share one fetch. 5. Wire pull to refresh as a forced request through the same repository call. 6. Test it: a repository test with a fake clock proves when it fetches; a view model test with a fake repository that emits twice proves the chart updates. After the change, the sync's write to the store reaches the chart with no extra code, because the chart was never reading anything else. ## What stays out of this decision The **policy values**, how long workout history stays valid, whether server cache headers or a push message invalidate it, are the caching design's subject. Writes the user makes offline, and how they are marked until the server accepts them, belong to the offline-sync design. This decision is narrower and comes first: **where** the rule lives, so that whatever policy is chosen is applied once.
- Pull to refresh must always hit the network; does that move the freshness decision back to the screen?No. The screen sends a request with a force flag, and the repository performs the fetch, writes the store and reports status. Non-forced reads still follow the repository's rule, and the screen sees the result through the same observed stream as any other change.
- Two screens observe the same history and both ask for a refresh at once. Who stops it firing twice?The repository. It tracks in-flight fetches per query and lets the second request join the first instead of starting another. Both screens observe the store, so one write updates both; no screen needs to know the other exists.
- How would you prove the fix with tests rather than by watching the screen?Test the repository with a fake clock, a fake server and a fake store: given data fetched recently, a read does not fetch; given old data, it does; a forced request always does. Test the view model with a fake repository that emits two values in a row, and assert the chart state follows the second.
- The view model should cache the last result and skip reloads to save calls.
- A one-shot read on screen open is enough because data won't change meanwhile.
- Each view model can keep its own copy as long as the expiries match.
- The screen should decide freshness because only it knows when it is visible.
- With freshness in the repository, pull to refresh can no longer force a fetch.