iOS Development

ios – SwiftUI navigation hyperlink infinite loop causes app to freeze

Spread the love


I’ve the next SwiftUI arrange

In my foremost, I’ve one thing like this

var physique: some Scene {
    WindowGroup {
        NavigationStack {
            if authStatusManager.isSignedIn {
                ProjectsView(projectsViewModel: projectViewModel)
            } else {
                LoginView(loginViewModel: loginViewModel)
            }
        }
    }
}

The primary time the person will see the LoginView however after logging in, we save the person token in person defaults and subsequently take the person on to the ProjectsView each time the app launches.

When the person efficiently logs in, we additionally take the person to the ProjectsView and it’s on this journey the place the issue arises. Right here is how the LoginView is ready up at a excessive stage:

var physique: some View {
    ZStack {
        // totally different login parts
    }
    .onChange(of: loginViewModel.apiCallState) { currentAPIState in
        if currentAPIState == .success {
            isLoggedIn = true
        }
    }
    .navigationDestination(isPresented: $isLoggedIn) {
        ProjectsView(projectsViewModel: loginViewModel.getProjectsViewModel())
    }

The initiatives view is an easy Record with some rows and works usually, the excessive stage code is one thing like this:

var physique: some View {
    ZStack {
        VStack {
            Record {
                ForEach(Array(projectsViewModel.projectRowViewModels.enumerated()),
                        id: .0) { index, projectViewModel in
                    Part {
                        NavigationLink(vacation spot: CurrentProjectView(currentProjectViewModel: projectsViewModel.selectedProjectViewModel(at: index))) {
                            ProjectRow(projectRowViewModel: projectViewModel)
                        }
                    }
                }
            }
            .id(UUID())
        }
    }
}

All the pieces is okay up till this level no matter if we come from the LoginView path or on to this view if the person is already logged in.

The problem arises once we go to the CurrentProjectView which can also be a listing

var physique: some View {
    content material
}

personal var content material: some View {
    Record {
        projectImageView
        
        ForEach(currentProjectViewModel.sections) { part in
            if part == .surveys {
                getSurveySection()
             }
    }
}

personal func getSurveySection() -> some View {
    ForEach(currentProjectViewModel.surveys.enumeratedArray(),
            id: .component) { index, subject in
        if index == 0 {
            // create a button
        }
        
        getNavigationLink(index: index, subject: subject)
    }
}

// This perform appears to be referred to as in an infinite loop once I faucet on the navigation hyperlink
@ViewBuilder
personal func getNavigationLink(index: Int, subject: String) -> some View {
    AnyView(NavigationLink(subject) {
        SurveyView(surveyViewModel: currentProjectViewModel.getSurveyViewModel(selectedSurveyIndex: index))
    })
}

So the final line of code goes into an infinite loop provided that we come from this path:

App Launch -> Login Display -> Initiatives Record Display -> Present Challenge Display
Tapping on the navigation hyperlink freezes the app and we have now an infinite loop

Nevertheless, if we come to the identical display after the person has already logged in and skipping the Login display, every thing works tremendous, that’s:

App Launch -> Initiatives Record Display -> Present Challenge Display
Tapping the identical navigation hyperlink features and takes the person to the appropriate vacation spot

I really feel it might have one thing to do with how I’ve arrange the navigation stack, nonetheless I can not appear to determine the problem.

Leave a Reply

Your email address will not be published. Required fields are marked *