M-Swift All articles
Swift Architecture

Diffing Is Not Mind Reading: A SwiftUI Identity Audit for Lists That Jump

M-Swift
Diffing Is Not Mind Reading: A SwiftUI Identity Audit for Lists That Jump

Photo: Nemo bis, GPL, via Wikimedia Commons

The ticket lands on a Tuesday: “the app reordered my stuff.” No screenshot, no repro steps, and support has already labeled it a sorting bug. Nine times out of ten it isn’t sorting at all. SwiftUI did exactly what you asked — you just told it the wrong thing about who each row is.

Identity Is the Contract You Never Signed

ForEach and List don’t compare your model objects field by field. They compare IDs. Give every row a stable, unique identity and SwiftUI can move a single row, keep its @State, keep a TextField focused, and animate the change the way a person expects. Give it an unstable identity and every refresh looks like a demolition followed by a rebuild.

What makes this nasty is that nothing crashes. The data is correct. The cells draw. It just looks wrong, intermittently, for the users with the longest lists and the most patience for filing tickets.

Five Ways an ID Goes Bad

Index-based identity

ForEach(items.indices, id: \.self) { index in
    ItemRow(item: items[index])
}

Insert one story at the top of a feed — routine for a news app in 2026 — and every existing row now has a new identity. Row state resets, focus drops, and the animation slides the whole list instead of one row. id: \.self over an array of String sections has the same flaw the moment two strings match.

IDs minted on the spot

struct Row: View {
    let item: Item
    private let id = [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier)()
}

That UUID is new on every render. SwiftUI sees a brand-new row each time and throws away the old one, along with anything the user typed into it.

Content-derived IDs

extension SearchResult: Identifiable {
    var id: String { title + subtitle }
}

Stable right up until a price updates, a badge count changes, or the ranking shifts. Then the same result becomes a different row and the list animates as if something appeared out of nowhere — which is exactly what the user sees.

Duplicate IDs

Pagination is the classic source: page one and page two both contain item 4471. SwiftUI’s diffing has no defined behavior for duplicates, so you get dropped rows, rows that swap on scroll, and jumps nobody can reproduce. Dedupe the array before it reaches the view.

Strings the user can change

Settings screens love id: \.self over an array of section titles. Switch the app language, or let someone rename a saved filter, and every section is a new section. The list rebuilds and any expanded state collapses.

What It Looks Like When It Breaks

An Afternoon Audit

Start with grep. Search for id: \.self and UUID() in view code; both deserve a second look every time. Then wrap your rows in a probe:

struct RowProbe<Content: View>: View {
    let id: AnyHashable
    @ViewBuilder var content: Content

    var body: some View {
        content
            .onAppear { print(id) }
            .onDisappear { print(id) }
    }
}

Refresh the list and read the log. Appending one item should print one appear and at most a couple of disappears. Fifty appears means your IDs churned, and you can stop reading this and go fix the model layer.

Second pass: the reverse test. In a debug build, reverse the array and reload. Rows should slide, not blink. Text fields should keep their contents. Expandable rows should stay expanded. If any of that fails, an ID is lying about who it is.

Third pass: the duplicate test. Append page one to itself and confirm SwiftUI renders a list you would recognize. Then check the same screen in a longer locale — German is a reliable stress test for settings lists that use display strings as IDs.

Animate Less, Blame Less

.animation(.default, value: items) attached to a List will happily replay every transition whenever identity churns. Animate the stable thing instead — a count, a selected ID, a sort key — and let row-level identity handle the rest. And if you ever want a full reset (importing a new document, switching accounts), reach for .id(accountID) on purpose. Deliberate resets are fine. Accidental ones are the bug.

Pre-Release Checklist

Row-jump tickets are cheap to prevent and expensive to diagnose after launch. Twenty minutes with a probe and a grep beats an afternoon spent reading session replays, trying to work out why a list quietly rearranged itself.

All Articles

Related Articles

Your App Group Is a Trust Boundary, Not a Folder

Friendly Fire From Your Network Layer: Auditing Swift Retry Logic Before the Bill Arrives

Friendly Fire From Your Network Layer: Auditing Swift Retry Logic Before the Bill Arrives

Picking a Local Store You Won't Regret: SwiftData and GRDB for Offline-First Apps

Picking a Local Store You Won't Regret: SwiftData and GRDB for Offline-First Apps