skip to content

In a mobile recipe feed mixing card sizes, sponsored cards and a horizontal carousel, where must each row's UI state live once views are recycled?

level: seniorimportance: should knowfreq 47%

answer

  1. a view is borrowed, not owned
  2. data, per-row UI state, transient state
  3. keyed by the item's id
  4. one pool per layout
  5. check the id when late work lands

basics

~20 s

Outside the view, keyed by the item's id: data such as the saved flag in the item's record, UI state such as a carousel's offset in a per-id map. Binding rebuilds the row from those, within a pool for its own view type.

solid answer

~50 s

A recycled view is borrowed by one item and then lent to another, so whatever it holds travels with it — the classic symptom is a heart saved on one recipe showing on another. Split the state three ways. **Item data**, like saved or a like count, lives in the item's record and the tap writes there first. **Per-row UI state**, like a carousel's scroll offset or an expanded section, lives in a map in the screen's state keyed by the item's id, saved when the row is unbound and restored on bind. **Transient view state**, like a pressed highlight, stays in the view and is reset on every bind. Keep one pool per view type, derived from the item's kind, so a view is only rebound to the same layout. And tag asynchronous work with the item's id, so a result that arrives after the view moved on is dropped.

go deeper

for a junior

Recall that a recycled view keeps whatever it last showed, so anything set on it for one item can appear on the next.

for a middle

Explain the three homes for state — the record, a per-id map, the view — and why each view type has its own pool.

for a senior

Diagnose leaked hearts and carousel offsets from symptoms, design the nested carousel's offset and pool, and guard late async results with an id check.

for a principal

Set a team rule that rows are rebuilt from the record and per-id state, and review new row types against it before they ship.

## The bug: state that lives in a recycled view In a recycled list, a **row view** is borrowed by one item and then lent to another. Anything stored only in the view travels with it. In the recipe feed the classic symptoms are: - the user taps the heart on one recipe, scrolls, and a different recipe's card shows a filled heart; - a collections carousel scrolled to its fifth card is followed further down by another carousel that opens at its fifth card; - an expanded "show ingredients" section appears open on a recipe nobody touched; - a label that finished formatting for the previous item lands on the new one. Each has the same cause: the view held state that belonged to an item, and binding did not replace it. ## Three kinds of state, three homes | state | example | home | after scrolling away and back | |---|---|---|---| | item data | saved, like count, title | the item's record in the store, surfaced through the screen's state | kept | | per-row UI state | carousel offset, expanded section | a map in the screen's state, keyed by the item's id | kept for the screen's lifetime | | transient view state | pressed highlight, a running animation | the view itself, reset on every bind | gone, by design | A tap on the heart writes the saved state to the record first; the card shows it because binding reads the record, never because the view itself was toggled. Binding then becomes a function of the item and its per-id UI state: given the same inputs, any recycled view produces the same row, and no view can carry one item's state onto another. ## Pools per view type Mixed rows need the list to know each row's **view type** — large card, small card, section header, carousel, sponsored card, 'cook again' row: 1. Derive the type from the item's kind, never from its position in the feed. 2. Keep one recycling pool per type, so a view is only rebound to a row of the same layout. 3. Keep the set of types small and stable; a type per sponsor creative fragments the pools until little is ever reused. 4. Bind every field the type displays, and clear the ones the item lacks. ## The nested carousel A horizontal carousel inside the vertical feed is a list inside a list, and it brings state of its own: - its **scroll offset** is per-row UI state: save it under the collection's id when its row is unbound, and restore it on bind; - its inner cards are virtualized and recycled horizontally, like the outer rows; where the toolkit allows, carousels of the same kind share one pool of inner card views — otherwise the carousel's own view, recycled with its row, keeps its inner views — so scrolling past many carousels does not build a fresh set for each; - its inner items need stable ids too, so an update to one collection does not rebind every card in it. ## Late results for a row that has moved on Work a row starts asynchronously — formatting, measuring, loading a preview — can complete after the view has been rebound to another item. Two guards keep the result off the wrong row: - tag the work with the item's id and, on completion, apply it only if the view is still bound to that id; - cancel work belonging to an item when its view is rebound, where the work can be cancelled. The image pipeline's own cancellation on reuse is part of that pipeline's design. ## Across toolkits The mechanism differs between list components: some reuse view objects, others re-create a row's content from a declaration, and state remembered inside such a row lasts only as long as the toolkit keeps that row, under whatever key the row was given. Neither guarantees that state stored inside a row survives scrolling or stays with its item, and a cross-platform framework adds a third mechanism with the same caveat. Testing for this class of bug is simple: save, expand or scroll something on one row, fling far enough for its view to be reused, and come back. The design that is safe on both is the same: data in the record, per-row UI state keyed by id outside the row, transient effects reset on bind, and nothing that matters stored only in the view.

  • Should the map of carousel offsets ever be cleared?
    Yes. It lives as long as the screen's state and should stay bounded: drop entries for items evicted from the local window, and clear it when the feed is reset. Keeping it forever grows memory with every collection scrolled past; losing an entry only means a carousel reopens at its start, which is acceptable.
  • Why not give every sponsored creative its own view type?
    Types exist to group rows that share a layout. A type per creative leaves each pool holding a view that is rarely reused, so the list ends up creating views as if it did not recycle. Model sponsored cards as one or two layouts and bind the creative's content into them.

  • Recycling copies state between rows of the same type, so leaks cannot be avoided.
  • Toggling the heart on the view is enough; the record can be updated later.
  • A row's view type can be worked out from its position in the feed.
  • An async result can safely be applied to whichever view requested it.
  • A carousel keeps its own scroll offset correctly while its view is recycled.