1

At the end of my application I have a Navigation link that sends the user to a screen awhile an API call is made.

The problem is that it does not seem that the function to send the API data is actually running when I push the Navigation link... what am I doing wrong here?

HStack {
             Spacer()
                            NavigationLink(destination: Success()
                                .navigationBarBackButtonHidden(true)){
                                Text("Finish")
                                    .foregroundColor(Color.newPrimary)
                                    .frame(minHeight: 40)
                                    .frame(width: 311)
                                    .background(Color.white)
                                    .cornerRadius(10)
                                    .padding(.top, 30)
                                }
                                .onTapGesture {
                                    print("Sending to Storage")
                                    SendData().sendData(apiData: data)
                                }
                            Spacer()
                        }               
Bnd10706
  • 1,933
  • 5
  • 24
  • 39
  • I tried this, and the function now runs, but the navigation link does not send me to the new screen. – Bnd10706 May 27 '22 at 19:54
  • The NavigationLink creates its own Button/Tap gesture, so adding another one leads to problems. Why not run your function at .onAppear of Success()? – ChrisR May 27 '22 at 21:01
  • Feel free to add to your question with your attempted code. Don't erase the old code, just add the new code. – Yrb May 27 '22 at 21:14
  • Because I have conditions for the api call that makes the success screen different. I need the api call to finish before the onappear works. – Bnd10706 May 29 '22 at 18:05

1 Answers1

3

I can think of a couple solutions. First is to use onAppear on your destination view. This is less code than the second solution, but I have seen issues with onAppear being fired at unexpected times or more than once.

NavigationLink {
    Success()
        .navigationBarBackButtonHidden(true)
        .onAppear {
            print("Sending to Storage")
        }
} label: {
    Text("Finish")
}

The other solution is to bind a variable to the isActive property of NavigationLink, and then use onChange(of:perform:) to watch for changes and trigger your function when the value changes to true.

struct ContentView: View {
    @State private var navIsActive = false
    
    var body: some View {
        HStack {
            Spacer()
            
            NavigationLink(isActive: $navIsActive) {
                Success()
                    .navigationBarBackButtonHidden(true)
            } label: {
                Text("Finish")
            }
            
            Spacer()
        }
        .onChange(of: navIsActive) { newValue in
            if newValue {
                print("Sending to Storage")
            }
        }
    }
}
Adam
  • 4,405
  • 16
  • 23