Problem with having Toggle inside a GeometryReader with onChange handler

Hi,

I've come across this weird issue which I think is probably a bug, but before I file a radar I'll ask here just in case I'm doing something in a weird way. I found that on macOS if I have a Toggle inside a GeometryReader inside a Tab, and I have an onChange handler for some property of the geometry (even if the onChange doesn't do anything) then the second time I switch to that tab, I get a whole lot of 'AttributeGraph: cycle detected through attribute' logs and the app hangs. It doesn't matter whether it's on the first or second tab, but I've put it in the first tab here.

This only happens on macOS… at first I thought it also happened on iOS, but it turned out that was a similar symptom caused by an unrelated issue.

Here is some code that reproduces the issue:

TabView {
			Tab("tab 1", systemImage: "rainbow") {
				Toggle("This toggle is fine", isOn: .constant(true))
			}
			Tab("tab 2", systemImage: "checkmark") {
				GeometryReader { geometry in
					VStack {
						//but with this toggle here, combined with the onChange handler,
						//the second time we switch to this tab we get the hang
						Toggle("This toggle causes a hang the second time we switch to this tab", isOn: .constant(true))
						//if we comment out the toggle and uncomment the text instead,
						//it's fine.
						//Text("This text does not cause a hang")
					}.onChange(of: geometry.size.height) {
						//even if this is empty, having it here makes
						//the app hang when switching to this tab for the second time.
						//and emit 'AttributeGraph: cycle detected through attribute' in the log
					}
				}
			}
		}
        .padding()

If I remove either the Toggle or the onChange handler, there is no problem. I can put all sorts of other things in the tab, but as soon as I put a toggle there, I get this hang. For now I've worked around it by putting Toggles in a settings sheet rather than directly on the tab, but since there's plenty of space on macOS it would be nice to have them directly on the tab.

One thing that's weird is that if I put this same code in the Settings window of an app, it doesn't seem to have the problem — maybe because the tabs are a different style there.

Answered by DTS Engineer in 863969022

That's an unexpected behavior and could be a bug. Please file the bug report and include a sample project that reproduces the issue. That would be helpful in Investigating.

Please post the Feedback ID number here so I can follow up.

Accepted Answer

That's an unexpected behavior and could be a bug. Please file the bug report and include a sample project that reproduces the issue. That would be helpful in Investigating.

Please post the Feedback ID number here so I can follow up.

Okay, reported as FB20866951

Problem with having Toggle inside a GeometryReader with onChange handler
 
 
Q