Search code examples
swiftswiftuirelationswiftui-navigationstack

NavigationStack with array from relationship


I am doing navigation stack for the array of related entries

var body: some View {
    NavigationStack(path: $navPath){
        VStack {
            List(Array(plant.event as! Set<Event>), id: \.self) { eventInList in
                NavigationLink(destination: EventPageView(event: eventInList)) {
                    EventCardRowView(event: eventInList)
                }.listRowBackground(Color("ChampagnePink"))
            }.navigationDestination(for: Event.self) { event in
                EventPageView(event: event)
            }
        }
    }
}

And this one doesn't work - all events are pushed to navigation stack at once - if i click on one event i just fall to the chain of all events consiquently.

Although it works perfectly correct, when i do it now with related entity, but as separetly fetched array:

NavigationStack(path: $navPath){
    VStack {
        List(events, id: \.date) { eventInList in
            NavigationLink(destination: EventPageView(event: eventInList)) {
                EventCardRowView(event: eventInList)
            }.listRowBackground(Color("ChampagnePink"))
        }.navigationDestination(for: Event.self) { event in
            EventPageView(event: event)
        }
    }
}

What's wrong, what is correct way to do that with relationship entry one-to-many?

EDIT: my qestion is different from CoreData one-to-many relationship. Problem with NSSet attribution

I dont have problem to fetch and view the list - i fetch related enries array and list it fine, but click on list item doesnt work correctly - whole array is pushed on stack at once - that means if i click on event 3 back button will bring me to event 2 , then 1


Solution

  • When Navigation stack is included or includes Form, Form triggers all array entries to be send on NavigationStack. So solution is just using them separetly