4

What's wrong with navigationDestination with binding on ios 16? I have an infinite loop in nested navigation with binding and navigation stack

struct Visit: Identifiable, Hashable {
    var id: String
    let name: String
}

struct ClientsListView: View {
    
    @Binding var selected: Visit

    var body: some View {
        Text(selected.name)
        .navigationTitle("Clients")
    }
}

struct VisitFormView: View {
            
    @Binding var selected: Visit

    var body: some View {
        NavigationLink("Select client", destination: {
            ClientsListView(selected: $selected)
        })
        .navigationTitle("Visit form")
        .onAppear {
            print("form on appear")
        }
    }
}

class VisitsViewModel: ObservableObject {
    @Published var list: [Visit] = {
        (0...30).map { Visit(id: $0.formatted(), name: "Test \($0)") }
    }()
}

struct VisitsView: View {
    
    @StateObject var vm = VisitsViewModel()

    var body: some View {
        List {
            ForEach(0..<vm.list.count, id:\.self) { index in
                NavigationLink(value: index) {
                    Text(vm.list[index].name)
                }
            }
        }
        .navigationDestination(for: Int.self) { id in
            let _ = print("VisitsView navigationDestination \(Date())") // infinite call print
            VisitFormView(selected: $vm.list[id])
        }
        .navigationTitle("Visits")
    }
}

struct MainView: View {

    var body: some View {
        TabView {
            NavigationStack {
                VisitsView()
            }
        }
    }
}

after I tap "select client" the result is an infinite loop, with a memory leak and 100% CPU

if I will remove binding from VisitFormView problem goes away. if replace navigationDestination to init(@ViewBuilder destination: () -> Destination, @ViewBuilder label: () -> Label) will work. I can resolve problems with the environment, but I want to understand what happens, and why loops. any help?

3
  • 2
    navigationDestination does not work with Binding you have to use NavigationLink with destination in it, hopefully there will be a better solution in a couple of weeks but as of no there is no solution for this just workarounds that half work. May 26, 2023 at 12:16
  • @loremipsum in apple GitHub foodtrack app they use binding in navigationDestination as in my example. In my example first level works fine, but stack second nested navigation . Yes navigation link works, but why stack and infinite loop happens, what calling loop
    – hitrain
    May 28, 2023 at 19:17
  • I’ve seen that example and hopefully Apple will have a better solution. Apple needs to implement something like the modals work. There is no viable solution their example does not work and it iterates over the entire collection way too many times. You’ll find a half dozen “solutions” on SO and people have open tickets trying to find a viable solution but there isn’t anything right now. May 28, 2023 at 20:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.