I have an odd issue and I’m not sure what I am doing wrong.
I have the following function that I want called in viewDidLoad to load all documents in a collection from Firestore that will be displayed in a tableview.
func functionName() -> [String] {
var NameArray = [String]()
db.collection("Names").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
NameArray.append("\(document.documentID)")
}
}
}
print(NameArray)
print(NameArray.count)
return (NameArray)
}
The function throws a warning result is being ignored. I don’t want to silence it as I need the value, it should return an array with the document names.
When I tried the below code, it returns the array and count as expected.
@IBAction func fetchUsersButton(_ sender: UIButton) {
var NameArray = [String]()
db.collection("Names").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
NameArray.append("\(document.documentID)")
}
}
print(NameArray)
print(NameArray.count)
}
}
However, I need to be able to return the array created so it can be used outside the function. Is anyone able to help?