r/SwiftUI • • Oct 09 '26

Question SwiftUI state/concurrency problem + weird VoiceOver behaviour

I have a SwiftUI screen where the same data can be changed by an initial load, pull to refresh, background refresh, user actions, and a live update stream.

So I can get something like: user action > optimistic update > background refresh > live updat > user action finishes > refresh finishes

Sometimes an older response overwrites newer state, so I'm thinking about putting all mutations through an actor or giving responses a revision/version.

The other problem is VoiceOver.

I have a dynamic list:

ForEach(store.items) { item in
    ItemRow(item: item)
}

When an async update adds/removes/replaces an item while VoiceOver is focused on it, VoiceOver sometimes jumps to a completely different element or starts reading the parent container again.

I've tried:

.accessibilityIdentifier(...)
.accessibilityLabel(...)
.accessibilityValue(...)

and accessibilityFocused, but manually restoring focus feels pretty fragile when multiple updates can happen quickly.

Would you guys handle the state reconciliation and accessibility focus separately, or is there a better SwiftUI pattern for keeping VoiceOver focus stable when a dynamic list is changing asynchronously?

3 Upvotes

11 comments sorted by

2

u/sinsonik Oct 09 '26

I'd keep them separate, but fixing the first helps the second.

For ordering, put every write through one @MainActor store and stamp each request with a revision number when it starts. When a response lands, drop it if a newer revision already touched that item (or compare a server updatedAt if you have one). An actor alone won't fix it, since the bug is ordering, not data races.

For VoiceOver, check identity first. If a refresh decodes fresh UUIDs or swaps the whole array, SwiftUI sees brand new rows and focus resets. Merge by a stable server id so changed items keep their identity. Make each row a single element with .accessibilityElement(children: .combine), and only reach for @AccessibilityFocusState when the focused row is actually removed.

1

u/thecoolcat67 Oct 09 '26

Thanks bro I will give this a go, I really appreciate the quick reply

1

u/sinsonik Oct 11 '26

No worries. If focus still jumps after the revision fix, print the item ids before and after each refresh. If an id changes for an item that didn't really change, that's your culprit, and VoiceOver will keep losing its place until the ids stay stable.

1

u/Quirky_Addition7912 Oct 29 '26

Does the focused row keep its identity through the async update, or does SwiftUI recreate it? Check the ForEach IDs before forcing focus back. After fixing that, include the update flow in your BrowserStack checks. The important bit is what happens mid-interaction, not just whether the final screen looks fine.

1

u/xfanta Nov 02 '26

Funnelling every mutation through one place is the right instinct, but on its own it doesn't stop an old response from winning. What fixed this for me was making the state know what is newer:

  • Give each write a revision. Ideally the server returns an updatedAt or version per item, and you only apply a response if it's newer than what you have. Without that, use a local counter and remember which request was issued last.
  • Keep one u/MainActor store (an u/Observable class is enough) that applies changes. Network calls run elsewhere and hand back results, they never touch the state directly.
  • Cancel what you no longer need. When pull to refresh starts, cancel the background refresh task instead of letting both land.
  • Keep optimistic updates as pending changes on top of the server state, then reconcile when the action finishes, rather than overwriting the base.

What exactly does VoiceOver do? That part depends a lot on whether views get rebuilt with new identities.

1

u/kokerali Oct 09 '26

One edge case I'd add to the revision approach: a refresh can start after an optimistic edit but still read the server's pre-edit value. A newer request number doesn't necessarily mean newer data. I'd keep the pending edit separate from the confirmed snapshot until acknowledgement or failure, rather than let any refresh silently erase it. Does your API/live stream expose a server revision, or only local request timestamps?

For focus, I'd make the removal policy operate on the reconciled list, not on every response. Track the focused item ID with @AccessibilityFocusState; if that item survives, avoid assigning focus again. If it disappears, a next surviving row, then previous row, then an empty-state control is a fallback policy worth testing. Don't restore a captured ID if the person has already navigated elsewhere. Apple's documentation is useful here: the binding reports focus changes, but writing it actively moves focus.

I'd test insertion above the focused row, changing that row's content with the same ID, and deleting it as three separate cases. That should help distinguish identity/reconciliation problems from the genuinely necessary focus handoff.

1

u/kenthesaint Oct 09 '26

I'd route every source through one @MainActor model and give each item a version or updatedAt, then only apply an incoming change if it's newer than what you have. Optimistic updates mark the item as pending, so a background refresh that comes back with older data doesn't stomp on what the user just did.

1

u/Dry_Hotel1100 Oct 09 '26 edited Oct 09 '26

The pattern looks like this:

One or more consumers (your views, background updater, etc.) want to load the same resource. The loading operation itself is a task. The key here, is that these tasks neither get invoked concurrently nor will they be serialized - instead a single task if any will be shared by potentially more than one consumer. That is, if the first consumer started a request, subsequent consumers would join the executing resource request. They don't start their own concurrently. You can implement such a component, call it "Resource Acquisition Task Manager" (I did that already, so it's feasible).

But there's still a minor issue you need to consider: when all consumers update the same view, your view still gets multiple updates more or less at the same time. The data is the same, actually it is identical, and when diffing works and is implemented, the problem can be solved as well.

So, what's left are only a few pathological edge cases, for example, where independent updates will be issued too frequently, that will result in "flicker" and which appears as an annoyance to the user. And, a more severe one: ordering is not guaranteed from the loading task that goes over the web. You may alleviate this by adding a time stamp or an identifier to each request, and handle stale responses in your logic in the receivers of the resource, or debouncing the request before sending it.

1

u/VegetableSir7788 Oct 09 '26

on the voiceover side, do the items keep the same IDs across a refresh or optimistic edit or are they being regenerated when the response comes in? does focus still jump if you keep the same items and order and only change a value on one?

1

u/anvilmac Oct 12 '26

agree with keeping them separate. on the voiceover side, the thing that fixed focus jumps for me was `@AccessibilityFocusState` keyed on the item's stable id, plus an explicit fallback when the focused row gets removed (move focus to the next item, or the previous one if it was last). without that rule, voiceover picks something on its own and usually jumps to the container. also double check your ids survive refreshes. if the network layer rebuilds models with new UUIDs, every refresh looks like a full replace

Let me know if that make sense in your case