Reputation: 606
I am having trouble with populating my table view. When I run this code it does not populate the table view at all. I am sure it is retrieving the data from firebase.
var restaurants: [Restaurants] = []
override func viewDidLoad() {
super.viewDidLoad()
videos = fetchData()
configureTableView()
}
here is my extension function:
extension RestaurantViewController {
func fetchData() -> [Restaurants]{
let db = Firestore.firestore()
db.collection("Restaurants").getDocuments { (snapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
return
} else {
for document in snapshot!.documents {
let name = document.get("Name") as! String
print("Name: ",name)
let restaurant1 = Restaurants(title: name)
self.restaurants.append(restaurant1)
}
}
}
return restaurants
}
I probably missing something simple, please assist
Thanks!
Upvotes: 1
Views: 408
Reputation: 1208
Try something like this instead
func fetchData(completion: @escaping (Bool) -> ()){
let db = Firestore.firestore()
db.collection("Restaurants").getDocuments { (snapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
completion(false)
} else {
for document in snapshot!.documents {
let name = document.get("Name") as! String
print("Name: ",name)
let restaurant1 = Restaurants(title: name)
self.restaurants.append(restaurant1)
}
completion(true)
}
}
}
In your viewDidLoad:
fetchData { (done) in
if done {
configureTableView()
} else {
// Handle this somehow
}
}
Upvotes: 3