skip to content

In a mobile news app's infinite feed, what load state stops one page being requested twice and keeps a failed load from ending the list?

level: middleimportance: must knowfreq 58%

answer

  1. the state guards, not the trigger
  2. idle, loading, failed, ended
  3. one in-flight request per direction
  4. apply a response only to its cursor
  5. failed is not ended

basics

~20 s

Keep one load state per direction outside the view: idle with a cursor, loading, failed or ended. Scroll triggers start a request only from idle, stale responses are dropped, and a failure shows an inline retry row, not the end.

solid answer

~40 s

Scroll callbacks near the end fire many times, so the guard cannot live in the trigger; it lives in a small state machine per direction, held in the view model or repository so a rebuilt screen keeps it. From `idle(cursor)` a trigger moves to `loading(cursor)` and sends one request; every other state ignores triggers. A response is applied only if the state is still loading that same cursor, so a late answer for a window that has since been replaced is dropped. Success writes the page and its next cursor together and returns to `idle(next)`, or to `ended` when the contract says there is no more. Failure moves to `failed(cursor)`, which renders an inline retry row and waits for a tap or for the connection to return, instead of retrying on every scroll event.

code

pseudocode · 27 lines
pseudocode
state.forward = idle(cursor = stored_next_cursor)

on near_end_trigger():
    s = state.forward
    if s.kind != "idle": return            // loading, failed or ended: ignore the trigger
    state.forward = loading(s.cursor)
    send_page_request(s.cursor)

on page_response(sent_cursor, page):
    s = state.forward
    if s.kind != "loading" or s.cursor != sent_cursor: return   // stale answer: drop it
    in one transaction:
        store page.items with their page sequence and position
        store page.next_cursor with the page
    if page.next_cursor is empty: state.forward = ended()
    else: state.forward = idle(page.next_cursor)

on page_failure(sent_cursor, error):
    s = state.forward
    if s.kind != "loading" or s.cursor != sent_cursor: return
    state.forward = failed(sent_cursor, error)    // list shows an inline retry row

on retry_tapped() or connection_restored():
    s = state.forward
    if s.kind == "failed":
        state.forward = idle(s.cursor)
        near_end_trigger()

go deeper

for a junior

Recall that scroll events near the end fire repeatedly, so the app must remember a page is already loading, and must never show the end of the feed after an error.

for a middle

Draw the four states and their transitions, say which state ignores triggers, and explain why a response is applied only when it matches the cursor being loaded.

for a senior

Place the state where it survives a rebuilt screen, keep directions independent, drop stale responses after the window changes, and pick retry signals that cannot hammer the server.

for a principal

Make the load state a shared component every feed in the app uses, so the guard, the retry row and the end footer behave the same everywhere and are tested once.

## Why the trigger cannot be the guard The near-end trigger in a feed is "the last visible row is within N rows of the last loaded row". It is evaluated on every scroll step, after every layout pass and whenever new rows arrive, so near the end it is true many times in a row. On a slow link the first request is still in flight while the trigger keeps firing. If each firing starts a request, the app asks for the same page several times, appends it twice, or races two responses into the store. Tricks applied to the trigger only lower the odds: - **Debouncing** delays and merges bursts, but a second burst after the delay still fires while the first request runs, and every legitimate load now waits for the delay. - **Unsubscribing from scroll events** while loading works until a screen rebuild re-subscribes with a fresh flag. - **Deduplicating rows by id afterwards** hides duplicates on screen but still pays for the extra requests and leaves two writers racing on the same cursor. The reliable guard is state the trigger consults, not a property of the trigger. ## The load state per direction Each paging direction gets its own small state machine. Forward paging is the main one; a window that has trimmed pages at its top may also page backward. Refreshing the head is a separate flow with its own rules. | State | Bottom row shown | What a near-end trigger does | |---|---|---| | `idle(cursor)` | Nothing, or the last cards | Moves to `loading(cursor)` and sends one request | | `loading(cursor)` | A loading row | Nothing | | `failed(cursor, reason)` | An inline row with the reason and a retry action | Nothing | | `ended` | An "all caught up" footer | Nothing | Only one transition sends a request, so one request per direction is in flight at a time. The cursor travels with the state, so the request and its response can be matched. ## Applying a response safely When a response arrives, the handler runs a short sequence: 1. **Check it still applies.** If the state is not `loading` with the same cursor that was sent, drop the response. That happens when the window was replaced or trimmed in the meantime, or when the screen was left and the request outlived it. 2. **Write the page and its next cursor in one transaction** into the local store the list reads from, so the list updates from the store and a kill cannot separate the two. 3. **Move on:** to `idle(next cursor)` if the contract returned one, or to `ended` if it signalled the end. On failure the handler applies the same check, then moves to `failed(cursor, reason)` with the cursor it was trying, so a retry repeats exactly that request. ## Failures and the end of the list - **A failure is never the end.** A tunnel, a timeout or a server error leaves more articles on the server; only the contract's end signal moves the state to `ended`. - **Show the failure where it happened**: an inline row at the bottom, below the cached cards, with a short reason and a retry action. A blank space looks like the end, and a full-screen error throws away articles the reader can still read. - **Retry on a clear signal**: the reader tapping the row, or the connection coming back. Retrying on every scroll event sends a request each time the reader nudges the list while still offline. - **Do not retry what will not succeed.** A rejected cursor needs a restart from the head of the feed, and a request refused for missing credentials waits for sign-in; neither is helped by backing off and trying again. ## Where the state lives The list view can be torn down and rebuilt: when the reader leaves the screen and comes back, when the process is recreated, and on one native platform by default when the device rotates. A flag stored on the view is lost each time and the guard silently resets. Keep the state in the layer that owns the feed's data, the view model or repository, and derive the bottom row from it. The stored cursor itself lives in the local database with its page, so a relaunch rebuilds `idle(cursor)` from disk, or `ended` if the end was recorded. ## Borders How a specific list component reports its visible range, and which callback fires near the end, are framework details. Choosing how far ahead the trigger fires is the page-size and prefetch decision. What this design owns is the state that makes a duplicate request impossible and keeps an error from being mistaken for the end.

  • Is debouncing the scroll trigger a substitute for the load state?
    No. Debouncing lowers how often the trigger fires but does not stop a second firing after the delay while the first request is still running on a slow link, and it adds that delay to every legitimate load. It can sit in front of the state as a cheap filter; the state is what makes a duplicate request impossible.
  • Why one in-flight request per direction rather than one for the whole feed?
    Because the directions are independent. A window that trimmed its top pages may need to load backward while the reader is also near the bottom, and a head refresh follows its own rules. A single global flag makes one wait on the other. Within one direction, though, two requests would fetch the same cursor twice or race to append, so one at a time is the rule.
  • After a failed page load, should the app retry automatically?
    On a clear signal, yes: when the connection returns, or with a bounded backoff for a transient server error. Not on every scroll event, which fires a request whenever the reader nudges the list while still offline. An error that retrying cannot fix, such as a rejected cursor or a request refused for credentials, needs a restart from the head or a sign-in instead.

  • Debouncing scroll events is enough to stop duplicate page requests.
  • A loading flag kept on the list view survives the screen being rebuilt.
  • Responses can be appended in arrival order without checking their cursor.
  • A failed page load should show the end-of-feed footer.
  • Retrying the failed page on every scroll event is a harmless retry policy.