简体   繁体   中英

Reload tableview with data from a Modal swift

Hello I try to access my data from my Modal to my ViewController and in my VC I try to Reload my Tableview but it does not work, but I get no errors. I have checked so that I get the data from my database (firebase) by print it my console.

Here is my code.
//MY ViewControlller

 // Load the user's contacts into the TableView (My ViewController)
    func loadContacts(willFilter: Bool){
        //contacts = MyContacts().contacts
        if willFilter {
            contactController.filterContacts()
        }
        contactTableView.reloadData()
    }

//My modal

import Firebase
struct MyContacts {​​​​​​​​

let db = Firestore.firestore()
let currentUser = CurrentUser()
let fav = ContactsViewController?.self
var filteredContacts : [Contact] = []

    var contacts : [Contact] {​​​​​​​​

        var myContacts = [Contact]()

let collection = db.collection("users").document(currentUser.email).collection("contacts")

        collection.getDocuments() {​​​​​​​​ (querySnapshot, err) in

            if let err = err {​​​​​​​​

print("Error getting documents: \(err)")

            }​​​​​​​​ else {​​​​​​​​

                for document in querySnapshot!.documents {​​​​​​​​

                    if let contactID = document.data()["id"] as? String, let contactUsername = document.data()["name"] as? String{​​​​​​​​

                        let contactEmail = document.documentID

                        let contact = Contact(username: contactUsername, email: contactEmail, id: contactID)

                        myContacts.append(contact)
                    }​​​​​​​​
                }​​​​​​​​

                DispatchQueue.main.async {​​​​​​​​

                }​​​​​​​​
            }​​​​​​​​
        }​​​​​​​​

        return myContacts
    }​​​​​​​​

}​​​​​​​​

can you try reloading it in the main thread:

 DispatchQueue.main.async {​​​​​​​​
   
    contactTableView.reloadData()
 }​​​​​​​​

and it's better to get your contacts in a completion instead of returning it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM