I am new to Swift and I am wondering why the array is empty at the end of the function at the print("array printed2: \(newArr)")
line, when at the first print statement print("array printed1: \(newArr)")
it is filled with data.
Does anyone have any corrections or suggestions on how to make the data in the array stay?
Thanks in advance, the code for the function is below.
func testRdArr(){
var newArr=[Establishment]()
print("Downloading club data...")
let db = Firestore.firestore()
db.collection("clubs").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
let data = document.data()
let name = data["Name"] as? String ?? "Name Unknown"
let description = data["description"] as? String ?? "Description Uknown"
let imageURL1 = data["imgUrl"] as? String ?? "Image Unknown"
let coordX = data["coordX"] as? String ?? "Unknown"
let coordY = data["coordY"] as? String ?? "Unknown"
let openingTimes=data["openingTimes"] as? [String] ?? ["Unknown"]
let weeklyEvents=data["weekly events"] as? [String] ?? ["Unknown"]
let postcode=data["postcode"] as? String ?? "Unknown"
let id = document.documentID
let x = Establishment(name: name, imageURL1: imageURL1, coordX: coordX, coordY: coordY, postcode: postcode, openingTimes: openingTimes, weeklyEvents: weeklyEvents, description: description, type: "club", id: id)
newArr.append(x)
}
}
//line below prints the populated array
print("array printed1: \(newArr)")
print("end of printing array")
}
//at this point array becomes empty, why does this happen and how would you suggest
//fixing it?
print("array printed2: \(newArr)")
}