Skip to content

fix(swift): store data model updates before notifying subscribers - #2323

Open
andrewkolos wants to merge 3 commits into
a2ui-project:mainfrom
andrewkolos:swift-datamodel-notify-after-store
Open

fix(swift): store data model updates before notifying subscribers#2323
andrewkolos wants to merge 3 commits into
a2ui-project:mainfrom
andrewkolos:swift-datamodel-notify-after-store

Conversation

@andrewkolos

Copy link
Copy Markdown
Collaborator

Issue

DataModel.data was @Published, and @Published emits during willSet: subscribers run before the new value is stored. SurfaceViewModel rebuilds the tree synchronously inside that emission, so any resolution that read back through dataModel.get saw the previous tree. This means that every property whose value is a FunctionCall object rendered one data update behind, while plain path bindings (resolved from the emitted value, not the model) stayed fresh, so one surface could show two generations of the same data in the same frame.

Change

DataModel now stores the update first and then emits, through a new dataPublisher. Subscribers that read back through the model get the value they were notified about, which makes DataContext's dataModel.get reads correct without touching them.

Tests that fail on the main branch are included.

API change

dataModel.$data is replaced by dataModel.dataPublisher. Migration is one line at each subscription site (two existed in this repo, both updated).

  • Emission still happens synchronously under the model's lock, as before; only the order of storing and notifying changed.
  • objectWillChange still fires before mutation, so SwiftUI observation of DataModel is unaffected.
  • Reads of data now take the model's lock; the old @Published stored-property read was unsynchronized against locked writes.

Pre-launch Checklist

One time:

For this PR:

- [ ] I have updated the relevant CHANGELOG.md file.

  • I updated/added relevant documentation.
  • My code changes (if any) have tests.
  • If my branch is on a fork, I have verified that scripts/e2e_test.sh passes.

If you need help, consider asking for advice on the discussion board.

DataModel.data was @published, which emits during willSet: subscribers
ran before the new value was stored, so resolution code reading back
through dataModel.get resolved every FunctionCall-valued property one
update behind, while plain path bindings, resolved from the emitted
value, stayed fresh. The data model now stores the update and then
emits through dataPublisher, so subscribers reading back through the
model see the value they were notified about.
gemini-code-assist[bot]

This comment was marked as resolved.

The separate storage property duplicated CurrentValueSubject's own
value, leaving two copies that had to stay in sync. Reads and the
read-modify-write in set() now go through the subject's value under the
same lock; the two initializers collapse into one with a defaulted
initial value. The ordering guarantee is unchanged and remains pinned
by DataModelTests.subscriberReadingBackThroughModelSeesStoredValue.
@andrewkolos
andrewkolos marked this pull request as ready for review August 18, 2026 17:54
@andrewkolos
andrewkolos requested a review from pinieb August 18, 2026 17:54
The parameter stopped being read when resolution moved into
DataContext, and with the data model now storing before it notifies,
the live reads inside DataContext match the tree the rebuild was
handed, so the parameter has no job left. Removing it also stops the
two action trigger closures from copying the whole data tree into an
argument that nothing read.
@andrewkolos
andrewkolos enabled auto-merge (squash) August 18, 2026 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants