Skip to content

Commit b6d0cf0

Browse files
authored
Fix nested State update issue (#538)
1 parent e0b090c commit b6d0cf0

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Sources/OpenSwiftUICore/Data/State/StoredLocation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ package class StoredLocationBase<Value>: AnyLocation<Value>, Location, @unchecke
3535
return false
3636
}
3737
box.$data.access { data in
38-
_ = data.savedValue.removeFirst()
38+
_ = data.savedValue.removeLast()
3939
}
4040
return true
4141
}

Tests/OpenSwiftUICompatibilityTests/Data/State/StateCompatibilityTests.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import Testing
66
import OpenSwiftUITestsSupport
77

8+
@MainActor
89
struct StateCompatibilityTests {
910
@Test
1011
func appear() async throws {
@@ -32,4 +33,41 @@ struct StateCompatibilityTests {
3233
)
3334
}
3435
}
36+
37+
@Test("StoredLocationBase.BeginUpdate.removeLast verify")
38+
func nestedStateUpdate() async throws {
39+
@MainActor
40+
enum Helper {
41+
static var values: [Int] = []
42+
static var valueChanges: [[Int]] = []
43+
}
44+
45+
struct ContentView: View {
46+
@State private var value = 0
47+
48+
var body: some View {
49+
let _ = Helper.values.append(value)
50+
Color.white
51+
.onAppear {
52+
value = 1
53+
value = 2
54+
withAnimation {
55+
value = 3
56+
value = 4
57+
}
58+
}
59+
.onChange(of: value) { oldValue, newValue in
60+
Helper.valueChanges.append([oldValue, newValue])
61+
}
62+
}
63+
}
64+
65+
try await triggerLayoutWithWindow(expectedCount: 0) { _ in
66+
PlatformHostingController(
67+
rootView: ContentView()
68+
)
69+
}
70+
#expect(Helper.values == [0, 2, 4])
71+
#expect(Helper.valueChanges == [[0, 2], [2, 4]])
72+
}
3573
}

0 commit comments

Comments
 (0)