[英]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.您好我尝试从我的模态访问我的数据到我的 ViewController 并且在我的 VC 中我尝试重新加载我的 Tableview 但它不起作用,但我没有收到任何错误。 I have checked so that I get the data from my database (firebase) by print it my console.
我已经检查过,以便通过在我的控制台打印它来从我的数据库(firebase)中获取数据。
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.并且最好让您的联系人完成而不是返回它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.