0

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

koen
  • 5,383
  • 7
  • 50
  • 89
user453575457
  • 464
  • 6
  • 21
  • Does this answer your question? [CoreData one-to-many relationship. Problem with NSSet attribution](https://stackoverflow.com/questions/68681841/coredata-one-to-many-relationship-problem-with-nsset-attribution) – lorem ipsum Apr 01 '23 at 15:46
  • 1
    I’m not great with SwiftUI but it looks like you are mixing both ways to use NavigationLink. You should either provide a destination with the link or use `.navigationDestination`. You are using both. https://developer.apple.com/documentation/swiftui/navigationlink – Geoff Hackworth Apr 01 '23 at 15:54
  • @loremipsum sorry but not, i tried List((plant.event?.allObjects as? [Event])!, id: \.date) and it give the same result - i fall into all events at once – user453575457 Apr 01 '23 at 15:55
  • @GeoffHackworth yes, i am using both, becouse i have custom button to jump to the details page. So both destination and navigation destination works here – user453575457 Apr 01 '23 at 15:58
  • You are specifying EventPageView as the destination in two different ways. I think there is a version of NavigationLink which takes a value (your event) and a view to use for the link (EventCardRowView). The .navigationDestination is responsible for declaring the detail view. – Geoff Hackworth Apr 01 '23 at 16:05
  • @GeoffHackworth thats deffinetly doesnt make difference, when i remove navigation destination it works the same. I dont jump twice to one event - i jump to all events at once, so if i click on event 3 back button will bring me to event 2 , then 1 – user453575457 Apr 01 '23 at 16:09
  • If you use a path for your navigation stack and .navigationDestination then you should not specify the destination again in the NavigationLink. I think you should use this version of NavigationLink https://developer.apple.com/documentation/swiftui/navigationlink/init(value:label:)-4jswo – Geoff Hackworth Apr 01 '23 at 16:13
  • @GeoffHackworth thanks, for the doc, but i already tried without navigationDestination, it doesn't make difference – user453575457 Apr 01 '23 at 16:18
  • I think you should be using navigationDestination. You should not be specifying EventPageView as the destination in your NavigationLink. This article has an example (using Text as the custom view for the link) at the end: https://swiftwithmajid.com/2022/10/05/mastering-navigationstack-in-swiftui-navigationpath/ – Geoff Hackworth Apr 01 '23 at 16:23
  • I just tried as you adviced: did NavigationLink(value: eventInList) , it work the same - navigates to all three eventsat once. – user453575457 Apr 01 '23 at 16:28
  • 1
    Without a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) it is impossible to help you troubleshoot. There is something little that is is being overlooked – lorem ipsum Apr 01 '23 at 16:30
  • Maybe the problem is because a completely new array is generated every time the view’s body is evaluated in the failing case? There is no stability in the id for each row of the list? – Geoff Hackworth Apr 01 '23 at 16:46
  • 1
    @loremipsum thanks a lot! after your comment i realized that problem is not in this exact piece of code, but higher. So finally i found a reason - this navStack view was rendered in Form { }, so taking it out of form, and rendering after - solved problem. thanks! – user453575457 Apr 01 '23 at 16:54
  • 1
    That would do it, `NavigationStack` should be at the very top. – lorem ipsum Apr 01 '23 at 16:55
  • 1
    @GeoffHackworth thanks a lot for your help! Problem is solved, the matter was - it was inside Form, outside form - it works as expected – user453575457 Apr 01 '23 at 16:55
  • @loremipsum eventually not, if i put form inside navStack it doesnt work either. So only separated form and navStack works. Not good news, but will try to style it nicely – user453575457 Apr 01 '23 at 18:38
  • @GeoffHackworth is correct here. You are using a view-destination `NavigationLink`, which pushes the view provided as the `destination` argument. `.navigationDestination` isn't causing any harm, but it's not doing anything at all. That may not solve the problem, but it's important to point out. – nteissler Apr 02 '23 at 18:10
  • @nteissler you are very wrong, .navigationDestination does the job, it allows me to navigate to any event-details not only by click on row, but by button, located in another view, and do some job before navigation. So when i press button "add event" - i can create event and navigate to it. – user453575457 Apr 02 '23 at 19:41
  • @Mira, I work on the the code for the `.navigationDestination` modifier. The example you shared doesn't include any value-destination `NavigationLink`s, nor any instances where you're appending an instance of `Event` to the `navPath`. If you have either of those elsewhere, then yes, the `.navigationDestination` modifier is serving a purpose. But not in the example you shared. – nteissler Apr 03 '23 at 23:59
  • @nteissler sure, example shared is just one app file, it doesn't include other app files/views where i append navStack. It's very strange to assume that thIs piece of code is all my app code. – user453575457 Apr 04 '23 at 08:29

2 Answers2

0

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

user453575457
  • 464
  • 6
  • 21
-1

It appears that your configuration of the NavigationStack and the NavigationLink may be the cause of the problem. Make sure that each NavigationLink is only in charge of navigating to one event when using a one-to-many connection rather than all of the events.

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)
        }
    }
}

might work

koen
  • 5,383
  • 7
  • 50
  • 89
rithuwa
  • 1
  • 7