skip to content

In a mobile recipe feed, why should list updates diff old and new items by stable id rather than by row position?

level: middleimportance: must knowfreq 58%

answer

  1. where it is versus what it is
  2. an insert above shifts every row
  3. inserted, deleted, moved, changed
  4. identity and content compared separately
  5. one id per row, even for repeats

basics

~20 s

Positions shift whenever an item is inserted, removed or moved, so updates by position rebind and animate the wrong rows. Diffing by a stable id finds exactly which items were inserted, deleted, moved or changed, and redraws only those.

solid answer

~50 s

A row's index says where an item sits now, and in a live feed that changes all the time: a new recipe at the top shifts every row below it. Updates aimed at positions therefore land on whatever moved into that slot — the wrong row animates, a save from a stale index hits the neighbour. A **stable id**, normally the server's id for the recipe, names the item itself, so a diff of the old and new id lists yields inserts, deletes and moves, and a separate comparison of content finds rows to update in place. Keep the two apart: if the whole record is the identity, a changed like count looks like a delete plus an insert, which replaces the row's view and drops state inside it. Ids must also be unique in the list — a recipe shown twice gets two row ids.

code

pseudocode · 13 lines
pseudocode
old_ids = ids_in_order(old_rows)
new_ids = ids_in_order(new_rows)

inserted = ids in new_ids and not in old_ids
deleted  = ids in old_ids and not in new_ids
kept     = longest_common_subsequence(old_ids, new_ids)
moved    = ids in both lists and not in kept

for id in ids in both lists:
    if displayed_fields(old_rows[id]) != displayed_fields(new_rows[id]):
        update_in_place(id)

apply(inserted, deleted, moved)

go deeper

for a junior

Recall that an index is where an item sits and an id is which item it is, and that inserts and removals shift every index below them.

for a middle

Explain the four results of a diff by id, and why content is compared separately from identity so an edit becomes an in-place update, not a delete plus an insert.

for a senior

Choose ids for every kind of row — server items, device drafts, headers, sponsored slots, a recipe shown twice — and keep large diffs off the UI thread and coalesced.

for a principal

Make stable identity a contract across the app: the API returns durable ids, drafts get client ids from creation, and no layer keys anything by position.

## Position is a location, not an identity A list's **position**, or index, says where an item sits right now. In a recipe feed it changes constantly: a fresh recipe arrives at the top, a sponsored card is placed at slot eight, a recipe the user hid disappears, the 'cook again' row is reordered. One platform's own guidance on diffing draws the line exactly: identifiers are stable and represent an item's identity, while indexes represent its location, which changes as items are added, removed and rearranged. An update applied by position therefore aims at whatever happens to sit there: - after an insert above, every row below shifts by one, so an update meant for one recipe lands on its neighbour; - an insert at the top, applied as "every row changed", animates dozens of rows instead of one; - a tap handler that remembered an index saves the wrong recipe if the list changed while the request was in flight. ## What a diff by stable id produces A **stable id** is a value that names one item for as long as it exists — normally the server's id for the recipe. **Diffing** compares the old list of ids with the new one and produces a small set of changes: 1. **Inserted** — ids present only in the new list. 2. **Deleted** — ids present only in the old list. 3. **Moved** — ids present in both whose relative order changed. 4. **Changed** — ids present in both whose displayed content differs; those rows are rebound in place. The list then animates exactly those rows and leaves every other row, its view and the reader's position alone. ## Identity and content are two separate comparisons | what is used as identity | an insert at the top | a like count changes | state inside the row | |---|---|---|---| | the row's position | every row below looks changed | right row only if nothing moved | attached to a slot, not an item | | the whole record | one insert | a delete plus an insert | lost on every edit | | the stable id, content compared separately | one insert | one in-place update | kept | Folding content into identity — treating the whole record as its own id — is the subtle form of the mistake. A changed like count makes the old item "disappear" and a new one "appear", so the row's view is replaced rather than updated. The same guidance spells out the cost: such changes turn into a delete and an insert, which can cost performance and can lose state inside the row, such as a running animation. Keep identity as the id and compare the displayed fields separately. ## Choosing the id - Use the **server's id** for server items — never the row index, and never a hash of the content, which changes whenever the content does. - Give an item created on the device a **client-generated id**, such as a UUID, from the moment it exists, so its row keeps its identity when the server acknowledges it; how temporary ids are reconciled belongs to the optimistic-update design. - Make ids **unique within the list**. A recipe shown in the 'cook again' row and again in the main feed needs two row ids — the section plus the recipe id, for example — because a diff cannot tell two copies of one id apart, and at least one platform's diffing step requires every id in the list to be unique. - Give rows that are not content an id as well: a section header its section key, a sponsored slot its placement id. ## Doing it at scale A diff over thousands of ids is cheaper than rebinding thousands of rows, but it is not free. Common practice is to compute it away from the UI thread where the toolkit allows and apply the result on it, and to coalesce bursts — a page arriving while a like count updates — into one diff rather than several in a row. Stable ids are also what make it possible to drop an item that arrives twice from overlapping pages; when merges deduplicate, and how fresh items join the top, is the refresh-and-merge design's subject. The list toolkit's own diffing utility and its keys are that toolkit's subject; the design decision here is what the identity is.

  • What goes wrong if the list is replaced wholesale instead of diffed?
    Every row is rebound and nothing animates, so the reader cannot see what changed; state inside rows, such as a carousel's offset, can be lost, and depending on the toolkit the scroll position can be reset or approximated. A full replace is fine for the first load or a reset of the data, not for incremental updates while someone is reading.
  • Which fields should the content comparison look at?
    The fields the row displays — title, image reference, saved state, like count. Comparing fields the row never shows triggers rebinds that change nothing on screen; leaving out a displayed field means its change never reaches the screen until something else happens to rebind the row.
  • How does a recipe drafted on the device keep its row when the server assigns its own id?
    Give the draft a client-generated id at creation and keep it as the row's identity — the server either accepts that id or the client maps the server's id onto it — so the acknowledgement arrives as a content change, not a delete and an insert. How the swap is done belongs to the optimistic-update design.

  • The row index is a safe key as long as the list is sorted.
  • Using the whole record as its own identity is safer because every change is caught.
  • A changed like count should be applied as a delete and an insert of the row.
  • One id can appear twice in the list if both copies are identical.
  • A hash of the item's content makes a good stable id.