In a mobile community-board app's cached feed, reply counts and edited titles arrive over a live socket; how should the client apply them to cached posts?
answer
- the store first, the screen follows
- patch fields, keep the position
- same id, new content: update the row
- unknown id: not the feed's business
- a server revision orders the updates
basics
~20 sPatch the cached post's changed fields in the local store by id and let the list update that one row in place. Keep its position, ignore ids the feed does not hold, and keep the higher server revision when copies race.
solid answer
~50 sA live edit is an update to one stored post, not a new item. The handler finds the post by its server id in the local store and patches only the changed fields — `replyCount`, `title` — in a transaction. The feed screen observes the store; because it diffs by **stable id**, it sees the same id with new content and re-renders that row in place: no delete and insert, no move. The client does not reorder the post: where it belongs is the server's ranking, which the next page will carry. An event for a post the feed does not hold is ignored by the feed, because an event does not say where a post belongs. And each post carries a server-issued **revision**, so a page fetched before the edit but landing after it cannot undo it.
go deeper
Recall that a live edit changes a post already on screen: update that post's fields where it is, and do not add it again.
Explain the path: patch by id in the store, the list observes and diffs by stable id, the row re-renders in place, unknown ids are dropped, revisions order the updates.
Cover what bites in production: rows that grow above the reader, bursts coalesced per id, older pages racing newer events, and why reordering is left to the next refresh.
Weigh live patches against periodic refresh for each field: what the connection, battery and server fan-out cost buys in freshness, and which counts truly need to be live.
## The scenario On a community board, numbers change constantly: a question about a broken streetlight collects replies every few minutes, a resident fixes a typo in an offer's title, a notice is marked "resolved". The app holds a live connection while it is in the foreground, and the server sends small events such as `{post 841, replyCount 17, revision 23}`. How the connection is opened, kept alive and reconnected is the connection layer's job. This question is about what the feed does with an event once it arrives. ## What an in-place edit must not do - **Insert a new row.** The post is already cached; a second row is a duplicate. - **Move the post.** A new reply may make the post rank higher on the server, but the client does not own the ranking. The next refresh will bring the new order; reshuffling on every event makes the list crawl under the reader. - **Flash or reset the row.** Deleting and re-inserting a row loses its state (an expanded preview, a playing animation) and draws attention to a change the reader may not care about. - **Refetch the page.** A page request per event turns a busy thread into a request storm and still has to be merged. ## The path from socket to screen 1. **Decode the event** and find the post by its server id in the local store. 2. **If the id is not stored, drop the event** for the feed. An event says what changed, not where the post sits in a ranked feed; inserting it would invent a position. If the post later arrives in a page, the page carries current data anyway. 3. **Compare revisions.** Apply the patch only if the event's revision is higher than the stored one. Arrival order is not write order: a page requested before the edit can land after it, and its older copy must not overwrite the newer count. 4. **Patch only the changed fields** in a transaction, leaving the feed ordering field alone. 5. **Let the list observe the store.** The list receives the new set of rows, diffs it by id, finds the same ids in the same order, and re-renders the one row whose content changed. With the store as the single source of truth, the same edit also reaches the post's detail screen, a saved list and any other surface that reads the post, without a second code path. ## Identity versus content in the diff How the list is fed decides whether an edit is an update or a delete-plus-insert: | What the list compares | Effect of an edited title | Effect of a new reply count | |---|---|---| | stable ids, content looked up per row | that row is reconfigured in place | that row is reconfigured in place | | whole post values as identity | the old value is deleted, a new one inserted | the old value is deleted, a new one inserted | | row indexes | the code must find the row's current index itself, and it shifts with every insert above | same | Platform guidance on lists diffed by identifier makes the same point: when items are stored as full values, any change makes the new value unequal to the old one, so the only way to show it is a delete and an insert per changed item; with stable identifiers, the list can be told to update just the changed item. ## Height changes and bursts - **A row that grows.** An edited title that wraps to a second line makes its row taller. If that row is above the reader, everything they are reading moves down by one line. The list must keep the first visible post anchored by id at its distance from the top — the same mechanism that protects the reader when posts are inserted above. - **Bursts.** A hot thread can produce several events a second. Coalescing events per id over a short window — 250 ms is a reasonable starting assumption, not a standard — and writing them in one transaction keeps the list from re-rendering many times per second. - **After a reconnect.** The feed does not depend on events missed while the connection was down; the next refresh brings current counts, because every page is merged by id with the same revision check. ## Why revisions, not clocks A revision number or server update time issued per post gives a total order for that post's versions. The phone's clock does not: it records when the phone received something, which is exactly the order that races get wrong, and device clocks drift.
- An edit event arrives for a post the reader has scrolled far past and the store has evicted; what happens?The id is not stored, so the feed drops the event. If the reader scrolls back and the page is fetched again, the page carries the current title and count, merged by id with the revision check, so nothing is lost.
- Should a post with many new replies move up the cached feed straight away?Not on the client's initiative. Ranking is the server's; the client cannot know where the post now belongs relative to posts it has not fetched. Patch the count in place, and let the next refresh bring the new order through the usual merge.
A departures board updates a flight's gate in its own line; it does not reprint the whole board or move the flight to the top because its gate changed.
- A live reply-count event tells the client where the post now ranks
- Apply live events in the order they arrive; the last is the newest
- Insert a post named by a live event when it is not cached
- Use the whole post's content as its identity when diffing the list
- Refetch the page every time a live event arrives