Skip to content

Commit 972fe63

Browse files
committed
chore: address concurrency warning on onPreferenceChange
In the SDKs released with Xcode 16.2, Apple marked this closure `@Sendable`, but *not* `@MainActor`. It's not clear if this is intentional or if it may be revised later. The official recommendation appears to be capturing the `Binding` and writing to it (`Binding` is asserted to be `Sendable`). https://stackoverflow.com/questions/79209071/xcode-16-2-beta-2-renders-onpreferencechange-unusable
1 parent a9c7615 commit 972fe63

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

Sources/MarkdownUI/Utility/ResizeToFit.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ private struct ResizeToFit1<Content>: View where Content: View {
3434
.preference(key: SizePreference.self, value: size)
3535
}
3636
.frame(width: size?.width, height: size?.height)
37-
.onPreferenceChange(SizePreference.self) { size in
38-
self.size = size
37+
.onPreferenceChange(SizePreference.self) { [$size] size in
38+
// Roundabout capture makes closure sendable (see #389)
39+
$size.wrappedValue = size
3940
}
4041
}
4142

Sources/MarkdownUI/Views/Blocks/BlockSequence.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ where
2626
VStack(alignment: self.textAlignment.alignment.horizontal, spacing: 0) {
2727
ForEach(self.data, id: \.self) { element in
2828
self.content(element.index, element.value)
29-
.onPreferenceChange(BlockMarginsPreference.self) { value in
30-
self.blockMargins[element.hashValue] = value
29+
.onPreferenceChange(BlockMarginsPreference.self) { [marginBinding = $blockMargins[element.hashValue]] value in
30+
// Roundabout capture makes closure sendable (see #389)
31+
marginBinding.wrappedValue = value
3132
}
3233
.padding(.top, self.topPaddingLength(for: element))
3334
}

Sources/MarkdownUI/Views/Blocks/ColumnWidthPreference.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ extension View {
99
)
1010
}
1111

12-
func onColumnWidthChange(perform action: @escaping ([Int: CGFloat]) -> Void) -> some View {
12+
func onColumnWidthChange(perform action: @escaping @Sendable ([Int: CGFloat]) -> Void) -> some View {
1313
self.onPreferenceChange(ColumnWidthPreference.self, perform: action)
1414
}
1515
}

Sources/MarkdownUI/Views/Blocks/NumberedListView.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ struct NumberedListView: View {
4141
)
4242
.environment(\.listLevel, self.listLevel + 1)
4343
.environment(\.tightSpacingEnabled, self.isTight)
44-
.onColumnWidthChange { columnWidths in
45-
self.markerWidth = columnWidths[0]
44+
.onColumnWidthChange { [$markerWidth] columnWidths in
45+
// Roundabout capture makes closure sendable (see #389)
46+
$markerWidth.wrappedValue = columnWidths[0]
4647
}
4748
}
4849
}

0 commit comments

Comments
 (0)