Learn Courses My Dashboard

Firebase getdocuments not fetching documents

I am fetching documents from firebase but it’s not working. any Good Samaritan who can guide me?

I call the fetch function while navigating to the home view

 NavigationLink(
                    destination: HomeView()
                        .onAppear(perform: {
                            //mandalo con informacion papaw!
                            model.getMatches()
                        }),
                    isActive: $goWhenTrue,

this is the view model fetch function

    func getMatches() {

        // Get the documents from the collection
        Firestore.firestore().collection("users").getDocuments { snapshot, error in
            if error == nil {
                
                var usuarios = [User]() //empty array of user instances
                
                for doc in snapshot!.documents {
                    
                    var u = User()
                    //q.id = doc["id"] as? String ?? ""
                    u.name = doc["name"] as? String ?? ""
                    u.birthdate = doc["birthdate"] as? Date ?? Date()
                    u.gender = doc["gender"] as? String ?? ""
                    u.height = doc["height"] as? Int ?? 0
                    u.latitude = doc["latitude"] as? Double ?? 0.0
                    u.longitude = doc["longitude"] as? Double ?? 0.0
                    
                    u.Q1day2live = doc["Q1day2live"] as? String ?? ""
                    u.QlotteryWin = doc["QlotteryWin"] as? String ?? ""
                    u.QmoneynotanIssue = doc["QmoneynotanIssue"] as? String ?? ""
                    u.bucketList = doc["bucketList"] as? String ?? ""
                    u.jokes = doc["jokes"] as? String ?? ""
                    
                    usuarios.append(u)
                }
                
                DispatchQueue.main.async {
                    self.users = usuarios
                }
            }
        }
    }

this is the model

class User {
    var name: String = ""
    var birthdate: Date = Date()
    var location: CLLocation?
    var latitude: Double?
    var longitude: Double?
    var gender: String = ""
    var sexuality: String = ""
    var datingPreferences: String = ""
    var height: Int = 0
    var Q1day2live: String = ""
    var QlotteryWin: String = ""
    var QmoneynotanIssue: String = ""
    var bucketList: String = ""
    var jokes: String = ""
    

I just realized that get documents is async (takes some time to come back) so I added a boolean var in the model to toggle as soon as the data is fetched…not fancy but it worked.

any other tricks from the seniors? I read about completion but I did not fully grasp the concept.

thanks