In a mobile community-board app's feed, a background refresh finds twelve new posts while the reader is forty posts down; how should they appear without moving the reader?
answer
- where is the reader right now
- at the top: insert; mid-feed: announce
- stage new posts in the store, unshown
- count only ids not yet shown
- anchor on an id, not an offset
basics
~20 sNew posts found while the reader is mid-feed go into the local store but stay behind a 'new posts' pill that reveals them at the top; anything inserted above keeps the first visible post's id at its on-screen distance.
solid answer
~50 sIt depends on where the reader is. At the top, or right after they pulled to refresh, insert the posts — they asked for them. Forty posts down, write the new posts to the local store but keep them above a *shown* marker the visible list does not cross, and show a `12 new posts` pill counted from ids not yet shown; tapping it moves the marker and scrolls to the top. If the design does insert above the reader — a live edit grows a row, or the list simply shows the whole store — the list must **anchor on an item**: before the change, note the first visible post's id and its distance from the top of the viewport, and restore that after it. Restoring a pixel offset or a row index lands on different content, because everything below the insert moved down.
code
pseudocode · 12 linesapply_update(list, new_rows):
anchor = list.first_visible_row()
if anchor == none:
list.show(new_rows)
return
distance = anchor.top - list.viewport_top
list.show(new_rows) // diffed by post id
target = anchor.id
if not new_rows.contains(target):
target = first id after anchor.id in the old order that new_rows still contains
if target != none:
list.scroll_so_row_top_is(target, list.viewport_top + distance)go deeper
Recall that inserting rows above a reader pushes their content down. New posts at the top are fine when the reader is at the top, and a pill is the usual answer otherwise.
Explain staging in the store behind a shown marker, counting the pill from unseen ids, and why an offset or index restore lands on different content.
Demonstrate one code path for every refresh: the network upserts, the list observes and diffs by id, and an anchor on the first visible post absorbs removals and resized rows above.
Frame the product trade-off: immediate freshness versus a stable reading position, which surfaces may auto-insert, and how one anchoring policy is enforced across every list in the app.
## The problem in one picture A resident opens the community-board app, scrolls forty posts into the feed and is reading a question about a lost bike. Meanwhile the app runs a background refresh — on return to the foreground, or because new posts were signalled — and the server's first page now holds twelve posts the phone has never seen. If those twelve rows are inserted at the top of the list while the reader looks at post 40, the content under their thumb is pushed down by twelve rows' height. The bike post vanishes below the fold, and the reader loses their place. The question is not *whether* to store the new posts — store them at once, deduplicated by id — but **when they become visible** and **what keeps the reader in place** when something above them changes. ## Two situations, two answers | Where the reader is | What to do with new posts | Why | |---|---|---| | At the top, or they just pulled to refresh | insert them at the top and show them | they asked for new content; a pill would cost a second tap | | Mid-feed, the refresh was automatic | stage them and show a "new posts" pill | inserting above would move what they are reading | | Mid-feed, and something above must change | apply it, anchored on the first visible post | an edit or removal above cannot be deferred forever | The pill is not a cosmetic choice. It turns "content moved under me" into "content is waiting, at my choice". ## The pill: stage, count, reveal 1. **Write the new posts to the local store immediately**, as an upsert by id, so they survive a kill and receive the same live edits as every other post. 2. **Keep a "shown" marker** — for example the feed position of the newest post the visible list includes. The list's query reads only posts at or below the marker; posts above it are staged. 3. **Count for the pill only ids that are not yet shown.** The fresh first page usually overlaps the cache, so "twelve new" is the number of ids above the marker, never the length of the page. 4. **On tap**, move the marker to the newest post and scroll to the top. Because the reader asked, the jump is expected. 5. **If the reader scrolls back to the top on their own**, reveal the staged posts automatically and drop the pill. Holding the posts in a memory-only buffer instead of the store looks simpler, but those posts miss the dedup and live edits the store applies, and they are lost if the app is killed before the reveal. ## If you insert above: anchor on an item, not an offset Sometimes content above the reader does change while they read: a post above them is removed by a moderator, or an edited title wraps to an extra line. The list must then keep the reader's content still. Three restore strategies are common, and only one works: - **Restore the previous scroll offset.** Every existing post moved down by the inserted rows' total height. With twelve rows at an assumed 120 px each, that is 1,440 px, so the old offset now shows posts the reader had already scrolled past. - **Restore the previous first visible row index.** Index 40 now holds what used to be index 28. Same failure, counted in rows. - **Anchor on the first visible post.** Before applying the change, record that post's **id** and its distance from the top of the viewport. After the change, find the row with that id and scroll so it sits at the same distance. Whatever was inserted, removed or resized above, the reader sees the same post at the same place. If the anchor post itself was removed, anchor on the next post below it that still exists. Whether a given list component does this for you, and through which setting, is that framework's own subject; the design requirement is the same on every platform. ## Keeping the store the single source Staging works because the list reads the store and the network only writes it. The refresh does not talk to the list at all: it upserts posts and, if the reader is at the top, also moves the shown marker. The list observes the query, diffs the result by stable id and applies inserts, deletes and moves. One code path serves pull-to-refresh, background refresh and app relaunch. ## Edge cases a design round probes - **A burst of refreshes** raises the pill's count; it never stacks pills. - **Staged posts already deleted** by a moderator before the reveal simply leave the count. - **A very long absence**, where the fresh page does not even reach the cached posts, is a gap rather than a refresh, and needs its own handling before any pill is shown.
- The reader taps the pill; what exactly happens to the store and the list?Nothing is fetched. The shown marker moves to the newest staged post, the list's query now includes the staged posts, the diff inserts them at the top, and the list scrolls to the top. The pill disappears because no ids remain above the marker.
- Why count the pill from staged ids rather than from the refreshed page's length?A refreshed first page usually repeats cached posts. Counting the page would announce twenty new posts when three are new, and a second refresh would count the same three again. Counting distinct ids above the marker stays correct across bursts.
- When is it acceptable to change content above a mid-feed reader without a pill?When the change must show now, such as a moderated post removed or an edited row growing, and the list anchors on the first visible post, so the reader sees no movement. New posts can wait, so they wait behind the pill.
- Restoring the previous scroll offset keeps the reader in place after an insert
- Restoring the first visible row index keeps the same post on screen
- Inserting above a mid-feed reader is harmless if it animates smoothly
- The pill count is the length of the refreshed page
- Keep staged new posts in a memory-only list beside the store