skip to Main Content

I know that this sounds a bit dumb, but how do you call a function only once. I have a tab bar at the bottom of my app, and every time that it is called, the name that I got from my firebase database, keeps on being added. For example, the name in firebase is Bob. The app for the first time will display Bob. Then you would click on the settings, and go back to the home view. Then the app will say BobBob, and over and over again. How do I make this stop.

Code:

    import SwiftUI
    import Firebase

    struct HomeView: View {
        
        @State var name = ""
        
        var body: some View {
            NavigationView {
                ZStack {
                    VStack{
                        Text("Welcome (name)")
                            .font(.title)

                        Text("Upcoming Lessions/Reservations:")
                            .bold()
                            .padding()
                        
                        Divider()
                        
                    }
                    
                }
                
            }
            .navigationTitle("Home")
            .onAppear(perform: {
                downloadNameServerData()
            })
            
            
        }
        private func downloadNameServerData() {
            
            let db = Firestore.firestore()
            db.collection("users").addSnapshotListener {(snap, err) in
                if err != nil{
                    print("(String(describing: err))")
                    return
                }
                for i in snap!.documentChanges {
                    _ = i.document.documentID
                    if let Name = i.document.get("Name") as? String {
                        DispatchQueue.main.async {
                            name.append(Name)
                            print("(name)")
                        }
                    }
                }
            }
        }
    }
        
    struct HomeView_Previews: PreviewProvider {
        static var previews: some View {
            HomeView()
        }
    }

2

Answers


  1. 
        import SwiftUI
        import Firebase
    
        struct HomeView: View {
            
            @State var name = ""
            
            var body: some View {
                NavigationView {
                    ZStack {
                        VStack{
                            Text("Welcome (name)")
                                .font(.title)
    
                            Text("Upcoming Lessions/Reservations:")
                                .bold()
                                .padding()
                            
                            Divider()
                            
                            
                            
                        }
                        
                    }
                    
                }
                .navigationTitle("Home")
                .onAppear(perform: {
                    downloadNameServerData()
                })
                
                
            }
            private func downloadNameServerData() {
                
                if !name.isEmpty { return }
                let db = Firestore.firestore()
                db.collection("users").addSnapshotListener {(snap, err) in
                    if err != nil{
                        print("(String(describing: err))")
                        return
                    }
                    for i in snap!.documentChanges {
                        _ = i.document.documentID
                        if let Name = i.document.get("Name") as? String {
                            DispatchQueue.main.async {
                                name = Name
                                print("(name)")
                            }
                        }
                    }
                }
            }
        }
            
        struct HomeView_Previews: PreviewProvider {
            static var previews: some View {
                HomeView()
            }
        }
    
    
    
    Login or Signup to reply.
  2. Did you consider only loading the name if you don’t have one yet?

    .onAppear(perform: {
        if (name == null) downloadNameServerData()
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search