In a mobile recipe feed, how should the client prepare rows just ahead of the viewport and cancel that work when the user flings past them?
answer
- rows already in the list, not pages
- speed times preparation time
- a fling is not a browse
- one task per item id
- bind copes without the prefetch
basics
~20 sPrepare rows in the scroll direction, a lead derived from scroll speed and preparation time, and cancel work for rows that leave that zone unseen. During a fast fling start nothing for passed rows, and let binding handle rows whose prefetch never ran.
solid answer
~50 sRow prefetch prepares items already in the list — text, layout, the start of a media fetch — before they scroll in; loading the next page of items is a separate mechanism. The lead comes from the scenario: scroll speed in rows per second times the time to prepare one row, plus margin, ahead in the scroll direction only. A fast fling passes far more rows than anyone sees, so while speed is above a fling threshold start nothing for passed rows, and prefetch around the predicted stopping point if the toolkit reports one. Cancel work for rows that leave the zone unseen, but keep anything already finished, never cancel a visible row's work, and share one task per item id so binding and prefetch never fetch twice. Binding must also cope with a row whose prefetch never ran: show a placeholder and start that work at high priority.
code
pseudocode · 26 lineson_scroll(visible_ids, direction, rows_per_second):
lead = ceil(rows_per_second * prepare_seconds) + margin
wanted = next_ids_after(visible_ids, direction, min(lead, max_lead))
if rows_per_second > fling_rows_per_second:
wanted = []
for id in keys(in_flight):
if id not in wanted and id not in visible_ids:
in_flight[id].cancel()
remove in_flight[id]
for id in wanted:
if id not in prepared and id not in in_flight:
in_flight[id] = start_prepare(id, priority = low)
on_prepared(id, result):
prepared[id] = result
remove in_flight[id]
on_bind(row, id):
if id in prepared:
show(row, prepared[id])
else if id in in_flight:
in_flight[id].raise_priority()
show_placeholder(row)
else:
in_flight[id] = start_prepare(id, priority = high)
show_placeholder(row)go deeper
Recall that prefetch prepares rows a little before they scroll in, and that work for rows scrolled past unseen should be cancelled.
Explain how to derive the lead from scroll speed and preparation time, and why it runs in the scroll direction only.
Handle flings, one task per item id, priority raises for rows that become visible, and binding that works when prefetch never ran.
Split the decision cleanly: the list owns which rows and when, while the network budget sets how much prefetch the device can afford.
## Two kinds of looking ahead An infinite feed looks ahead twice. Loading the **next page** adds items to the local list when the reader nears the end of what is loaded; that trigger and its distance belong to the paging design. **Scroll prefetch**, the subject here, prepares rows whose items are already in the list but not yet on screen: formatting their text, measuring their layout and starting the fetch of their images or previews, so that binding finds the content ready when the row scrolls in. Some list components offer a prefetch hook for this; where none exists, the client computes the zone from the visible range itself. The guidance for one such hook states both halves: start loading what a row will need before it is shown, and cancel that work when the list says it is no longer needed. ## Deciding how far ahead No distance is a standard; derive it from the scenario and label the inputs as assumptions: 1. Measure, or assume, how long preparing one row takes — mostly its media fetch on the scenario's network. 2. Estimate the reader's normal scroll speed in rows per second. 3. Set the lead to speed times preparation time, plus a margin, in the scroll direction only. With assumed numbers: a card is a quarter of the screen, a browsing reader moves about one screen a second — four rows a second — and a recipe photo takes about half a second to arrive. A lead of two rows just covers it; four to six leaves margin for a slower network. Rows behind the reader were usually prepared on the way down; when the reader reverses, the scroll direction, and with it the lead, simply points the other way. ## What preparing a row includes Preparing is whatever binding would otherwise do in the moment the row appears, ordered by how much it saves: - **media fetches**, the slowest part, started at a low priority that a visible row can outrank; - **layout measurement** for rows whose height depends on their content, so the row arrives at its final size; - **formatting** of text that costs real work, such as relative dates, counts and highlighted search terms. Cheap work — setting a title from a record that is already in memory — gains nothing from being done early. Prepared results go into a bounded cache keyed by item id; how that cache is sized against the device's memory is the cache design's decision. ## A fling is not a browse A fast fling can pass dozens of rows a second. Preparing every row it passes starts work for rows nobody will see, and that work competes with the rows the reader will land on: - while the scroll speed is above a fling threshold, start no new prefetches for rows being passed; - where the toolkit reports a predicted stopping point, prefetch around that point instead; - when the scroll slows to browsing speed, return to the normal lead. ## Cancelling, and what cancel means Prefetched work is cancelled when its row leaves the prefetch zone without being shown — the reader reversed direction or flung past. Three rules keep that correct: - **Cancel is a hint.** Work that already finished is kept in its cache, so a row revisited later is ready. - **Never cancel a visible row's work.** If a row becomes visible while its prefetch is in flight, raise that task's priority instead of starting a second request. - **One task per item id.** Prefetch and binding look up the same in-flight task, so a row is never fetched twice. ## Binding must not depend on prefetch The same guidance warns that the prefetch call is not made for every row, so the display path has to cope with three cases: the content is ready, it is still being fetched, or it was never requested. | case at bind time | what the row does | |---|---| | prepared | show the content immediately | | in flight | raise its priority, show a placeholder of the final size | | never requested | start it at high priority, show the placeholder | Designed this way, prefetch only ever makes rows appear sooner; turning it down, or off, never makes a row wrong. ## Boundaries How an image is decoded, downsized and cached is the image pipeline's design; how many videos ahead to preload, and the player pool, belong to the video design; what this costs in radio time and data, and how a metered connection changes the lead, is the network budget's decision. Here the decisions are which rows, how far ahead, and when to stop.
- Should row prefetch run on a metered or slow connection?That is mostly a budget decision: the list exposes its lead as a setting, and a network policy lowers it — to text and layout only, for example — when data is metered or the link is slow. The list's own job is to stay correct at any lead, which the three bind-time cases already guarantee.
- Why not prepare every row of a page as soon as that page arrives?A page can hold far more rows than the reader will scroll through, and many readers leave after a few screens. Preparing by distance from the viewport spends work on rows likely to be seen; preparing by page spends it on rows that may never be reached, and gives scrolling nothing to cancel.
- Row prefetch and loading the next page are the same mechanism.
- Every row passed in a fling should be prefetched in case the user scrolls back.
- Binding can assume the prefetch already ran for its row.
- Cancelling a prefetch throws away work that had already finished.
- A visible row should start a fresh request even if its prefetch is in flight.