Skip to content

Show streamed text in a lazy stack row that SwiftUI has not placed - #30

Merged
servercimen merged 1 commit into
mainfrom
feature/stream-row-height
Nov 24, 2026
Merged

servercimen merged 1 commit into
mainfrom
feature/stream-row-height

Conversation

@servercimen

Copy link
Copy Markdown
Owner

Summary

A TranscriptText(stream:) added to a LazyVStack could stay empty (#12). After 50 rows of 1,400 points, the answer row never showed.

  • Cause. The lazy stack added the row without placing it. While a row is not placed, the view asks SwiftUI to measure it again through its own @State (Measure streamed text in views that SwiftUI has not placed #15). In this layout, SwiftUI did not act on that state. A trace on the phone shows no update and no measure of the row after SwiftUI made it, while the text grew to 1,197 points. TranscriptText(document) with the document in the parent's @State showed the answer: each parent update made SwiftUI update and measure the row.
  • Fix. The stream gets an internal observable revision. TranscriptText(stream:) reads it and keeps it when it is made, so the revision is a dependency of the view whose body makes the TranscriptText. While the row is not placed, each new height changes the revision. That view then runs again and gets a different TranscriptText value, and SwiftUI updates, measures and places the row. The public API does not change.
  • Limit. This works when TranscriptText(stream:) is made in the ForEach closure or in the view that holds the stack. In the body of a row view of its own, the change stays inside the row, and the row can stay empty. In the same layout, SwiftUI Text that reads an observable model in a row view of its own also stayed empty. The README says where to make the view.

Refs #12.

Tests

  • swift test: 179 tests pass, with 1 new test. It checks only that the revision is observable. The device repros below cover the rest of the path. The package builds for iOS with no warnings.
  • Repros on an iPhone 12 mini with iOS 27.0, optimized build:
Case main This PR
Repro 1: an empty row, with text a second later shown, 2 of 2 shown, 2 of 2
Repro 2: an answer after 50 rows of 2,432 pt shown, 2 of 2 shown, 2 of 2
Repro 2: an answer after 50 rich TranscriptText rows shown, 2 of 2 shown, 7 of 7
Repro 2: an answer after 50 rows of 1,400 pt not shown, 2 of 2 shown, 7 of 7
The same, with the answer as the last ForEach row not shown, 2 of 2 shown, 7 of 7
The same, with the answer in a row view of its own not shown, 2 of 2 not shown, 2 of 2
The same, with the answer after the LazyVStack shown, 2 of 2 shown, 2 of 2
  • In the 1,400-point cases, the view that makes the answer ran 2 to 4 more times before SwiftUI placed the row. After that, the row grows through invalidateIntrinsicContentSize, as before.
  • Streaming cost: in the history bench (an answer of 6,000 characters after 50 messages), the main-thread CPU is 1.85 s to 2.15 s, against 2.03 s to 2.15 s on main. The tile draws (70 to 71), the memory peak (48 MB) and the dropped frames (0) do not change. That answer row is placed, so the bench does not run the new path. It shows that the usual path did not change.
  • Not checked: the issue's iPhone 17 Pro with iOS 27.0.1.

🤖 Generated with Claude Code

A `TranscriptText(stream:)` added to a `LazyVStack` could stay empty.
After 50 rows of 1,400 points, the answer row never showed. The lazy
stack added the row without placing it. The view asked SwiftUI to
measure it again through its own state, but in this layout SwiftUI
ignored changes inside the row.

Now the stream has an observable revision. `TranscriptText(stream:)`
reads it and keeps it when it is made. While the row is not placed,
each new height changes the revision. The view whose body made the
`TranscriptText` then runs again and gets a different value, and
SwiftUI updates, measures and places the row.

This works when `TranscriptText(stream:)` is made in the `ForEach`
closure or in the view that holds the stack. In the body of a row view
of its own, the change stays inside the row, and the row can stay
empty. In the same layout, SwiftUI `Text` that reads an observable
model in a row view of its own also stayed empty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@servercimen
servercimen merged commit f793d1d into main Nov 24, 2026
2 checks passed
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.

1 participant