In a mobile art-museum app's fast-scrolling grid, cells briefly show the wrong painting before the right one appears; what causes it, and how should the image pipeline prevent it?
answer
- the cell outlives the request
- a late result, a new binding
- clear the cell on rebind
- detach, cancel only if no one waits
- check the key at delivery
basics
~20 sA recycled cell gets a new artwork while its old request still runs, and the late result lands in it. On rebind, clear the image and detach the old request; at delivery, drop results whose key no longer matches.
solid answer
~50 sGrids recycle cells: the view that showed painting A is reused for painting B as the visitor scrolls. If A's download and decode finish after the rebind, a naive completion sets A's pixels on a cell now showing B - and if B's image is slower, A stays there. Three guards fix it. On rebind, reset the cell to its placeholder so it never shows stale pixels. Detach the cell from A's request, cancelling the work only if no other cell is waiting on it. And on delivery, compare the result's key with the key the cell holds now and drop a mismatch, because cancellation can race with a result already queued for the UI thread. Finally, lower the priority of work for cells that scrolled away so it does not delay the visible ones.
code
pseudocode · 15 linesfunction bind(cell, artwork):
key = artwork.imageUrl + "@" + cell.pixelSize
if cell.request != null:
cell.request.detach(cell) # cancels the job only if no consumer is left
cell.requestKey = key
cell.showPlaceholder()
cell.request = pipeline.load(key, priority = "visible", consumer = cell)
function deliver(cell, resultKey, pixels): # runs on the UI thread
if cell.requestKey != resultKey:
return # late result for an old binding
cell.show(pixels)
function onScrolledAway(cell):
cell.request.setPriority("low")go deeper
Know that grid cells are reused for different items, and that an image arriving late can land in a cell that now shows something else.
Explain the timeline of the race and the three guards: reset the cell, detach from the old request, and compare keys before showing a result.
Show why cancellation alone loses the race, how detaching interacts with coalesced jobs, and when to cancel, demote or let a nearly finished download complete.
Put the binding and the delivery check inside the shared pipeline so no screen can forget them, and make the race reproducible in testing with throttled, uneven networks.
## How recycling produces the wrong painting A grid of thousands of artworks does not create thousands of views. It creates enough cells to fill the screen plus a few, and **recycles** them: a cell that scrolls off the top is rebound to the item about to appear at the bottom. Recycling is what keeps memory flat, but it means a cell's lifetime no longer matches any one image request. The bug unfolds like this: 1. Cell 7 is bound to painting A and asks the pipeline for A's image. 2. The visitor flings; cell 7 scrolls off and is rebound to painting B, which asks for B's image. 3. A's download and decode finish first, and its completion handler sets A's pixels on cell 7. 4. Cell 7 now shows A under B's title, until B's image arrives - or for good, if B's request failed. On a good connection the window is a few frames and looks like a flicker. In a museum gallery with weak signal, downloads take seconds, so the wrong painting can sit there long enough for a visitor to read it as the real one. ## Three guards, and why all three - **Reset on rebind.** The moment a cell is bound to a new item, replace its image with the placeholder. Without this, the old painting stays visible until the new one arrives, even with no late result at all. - **Detach from the old request.** The cell tells the pipeline it no longer wants A. Because requests are coalesced, A's job may serve other cells too, so detaching removes this cell from the job's list of consumers, and the job is cancelled only when that list is empty. - **Check the key at delivery.** When a result arrives on the UI thread, compare its key with the key the cell holds now. If they differ, drop it. The key check is the guard that cannot be skipped. Cancellation is cooperative: by the time the cell detaches, A's result may already be decoded and queued for the UI thread, and a coalesced job still delivers to whoever registered. Only the check at delivery sees the cell's final state. ## Cancel, demote or let it finish? When a cell detaches, the right action depends on how far the work has got and who else needs it: | State of the old work | Sensible action | |---|---| | Queued, not started, no other consumer | Cancel it outright | | Downloading, another cell still waiting | Keep it; only this cell detaches | | Downloading, no consumer left, nearly finished | Often let it finish to disk at low priority, since the bytes may be wanted again soon | | Downloading, no consumer left, barely started | Cancel it and save the data | | Decoding for a size nobody wants now | Cancel if the decoder allows it; otherwise discard the result | The middle rows are judgment calls, and "nearly finished" is a threshold you choose and measure; there is no universal figure. ## Priority follows visibility - **Visible cells first.** Requests for cells on screen go ahead of requests made for rows prepared ahead of the viewport. - **Demote what scrolled away.** Work for a cell that left the screen drops to low priority instead of competing with the cells the visitor is looking at. - **Promote on return.** If the visitor scrolls back, the same key is requested again, joins the in-flight job and raises its priority. When the list starts preparing rows and how far ahead it reaches is the list's own prefetch decision; the pipeline's part is to honour the priority it is given and to change it when visibility changes. ## Reproducing and guarding against it - **Make it slow and uneven.** Throttle the network with varied latency and fling the grid; the bug needs a late result, so a fast network hides it. - **Make it visible.** In debug builds, draw each cell's bound item id and the delivered image's id; any mismatch is a bug. - **Keep the binding in one place.** The cell stores exactly one current key, set at bind time; every completion goes through the same check rather than each screen writing its own.
- Why not cancel the old request on rebind and skip the key check?Cancellation races with completion. The old result may already be decoded and waiting on the UI thread's queue when the cancel arrives, and with coalescing the job keeps running for other cells and delivers to every consumer it has registered. The key check at delivery is the only guard that sees the cell's binding at the moment the pixels would be set.
- The visitor flings past two hundred artworks; what should happen to their in-flight work?Each cell detaches as it is rebound, so most jobs lose their only consumer. Queued jobs are cancelled before they start; downloads that barely began are cancelled; nearly finished ones may complete to disk at low priority. No decode should run for a size nobody is waiting for. The visible cells' requests then run first instead of queuing behind two hundred abandoned ones.
- Why reset the cell to a placeholder on rebind if the key check already exists?The key check stops a late result from being shown, but it does nothing about the pixels already in the cell. Without a reset, a recycled cell keeps showing the previous painting until the new one arrives, which on weak signal can be seconds. The reset makes the cell honest from the moment it is rebound.
- Cancelling the old request on rebind is enough on its own to stop wrong images.
- A cancelled request's completion can never run, so no check at delivery is needed.
- Cancel a shared download as soon as one of its cells scrolls away.
- Keep the previous image in a recycled cell until the new one loads.
- Turning off cell recycling is the right fix for a grid of thousands of images.