I'm calling a function, that function initializes a class, and in that class I have an initialize method that does a request to Firestore. I also have a variable in that class, in which once I'm done looping through the database, I change this self.variable to equal whatever I just created. For some reason, I am not able to get this to finish, and when I initialize my class object, the value of the variable never changes.
(Assume User class consists of just name and age)
This is where I call my function and initialize my object
fileprivate func fetch(){
let homeObject = Home()
print (homeObject.user.count)
//count prints out as 0
print (homeObject.test)
//test prints out as 2 .. should be 3 but I'm not sure why?
}
This is the class for Home
class Home{
var user: [User] = []
var test = 1
required init() {
self.test = 2
var userLoop = [User]()
let db = Firestore.firestore()
db.collection("users").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
let name_data = document.data()["name"]! as! String
let age_data = document.data()["age"]! as! String
let userTemp = User(name: name_data , age: age_data)
self.test = 3
userLoop.append(userTemp)
}
self.user = userLoop
}
}
}
For some reason, test is able to change to the number 2, but I believe this initializer method doesn't finish, as it is doesn't go to the number 3 (as you see above, later in the initializer function I change it to the number 3). I would really appreciate the help, been stuck on this for hours now!