In the following example code, I am trying to update the value of an Observable object. But some how this triggers an infinite loop of updating globalValue.value
:
import SwiftUI
@Observable class GlobalValue
{
var value: Double = 0
}
let globalValue = GlobalValue()
struct TestLayout: Layout
{
func sizeThatFits(proposal _: ProposedViewSize, subviews _: Subviews, cache _: inout ()) -> CGSize { .zero }
func placeSubviews(in bounds: CGRect, proposal _: ProposedViewSize, subviews: Subviews, cache _: inout ())
{
globalValue.value += 1
print(globalValue.value)
}
}
struct ContentView: View
{
var body: some View
{
TestLayout { Text("A") }
}
}
#Preview
{
ContentView()
.padding(50)
}
What is causing the infinite loop when using @Observable
?
Edit: Deleted ObservableObject
example. That one was a mistake.