NavigationLink Clears my 'selection'

I am creating a Sidebar-style view for an iPad app, using the NavigationLink(destination:tag:selection:label) to control the detail view. Visually, everything appears to be working correctly, however a subtle issue exists when tapping an item to change the selection. The contents of my List are being processed twice, which is observed by putting a breakpoint at the first Text component in the code below - the first time through, my selectedHomeId value is populated correctly (and does load the HomeView correctly), however the second time through, the selectedSignalId is nil.

This does not impact the HomeView, and the app is still usable, however it does clear out the SceneStorage that I'm attempting to configure, and it results in the HomeView not being able to respond to changes in my model.

Code Block
@SceneStorage("selectedHome")
var selectedHomeId:String?
var sidebarType = SidebarType.full
var body: some View {
List {
if sidebarType == .full || sidebarType == .homes {
Text("Homes")
.font(.headline)
ForEach(homeStore.homes) { home in
NavigationLink(destination: HomeView(home: home), tag: home.id.description, selection: $selectedHomeId) {
Label(home.name, systemImage: "house")
}
}
}
}
.listStyle(SidebarListStyle())
.navigationTitle(sidebarType == .homes ? "Homes" : "Other")
.navigationBarItems(trailing: SettingsButtonView())
}


I've also filed a feedback for this (FB7845359) - if any other developers have experienced this, please file your own as well!

Replies

Please try to replace body with sidebar, which looks like the following code:

Code Block
var sidebar: some View {
List {
if sidebarType == .full || sidebarType == .homes {
Text("Homes")
.font(.headline)
ForEach(homeStore.homes) { home in
NavigationLink(destination: HomeView(home: home), tag: home.id.description, selection: $selectedHomeId) {
Label(home.name, systemImage: "house")
}
}
}
}
.listStyle(SidebarListStyle())
.navigationTitle(sidebarType == .homes ? "Homes" : "Other")
.navigationBarItems(trailing: SettingsButtonView())
}
var body: some View {
NavigationView {
sidebar
ContentView()
}
}


The only functional difference I can see in the code above is to ensure that the enclosing NavigationView (which was not in my example, but is a layer up) has a second View inside it, representing the 'Default' view that is shown, prior to taping one of the NavigationLink. It didn't work.

The observed behavior is still that each time I tap one of the NavigationLinks in the sidebar the sidebar is refreshed twice -- once with the selectedHomeId populated properly, and again with the selectedHomeId set to nil. Visually, everything looks fine -- the second refresh of the side bar does not clear out the detail view.
I had the same issue and can confirm that Apple_Kevin's response fixed the Attribute Graph cycle
I've spent some time on this again, reworking as Apple_Kevin suggests, but the issue still occurs for me. Exists in Beta 2 as well
I'm seeing the same thing in a very simple test app. A NavigationLink with a bound selection inside a List will set the selection binding to nil immediately after setting it correctly.

FB7868682 filed.
This one is still an issue in Beta 3