I try to get the values from Firebase and after that I want to print the second line of code executed and then true.
But the order is total wrong. What am I doing wrong?
override func viewDidLoad() {
super.viewDidLoad()
method(arg: true, completion: { (success) -> Void in
print("Second line of code executed")
if success { // this will be equal to whatever value is set in this method call
print("true")
} else {
print("false")
}
})
}
func method(arg: Bool, completion: (Bool) -> ()) {
let userID = Auth.auth().currentUser!.uid
let database = Database.database().reference()
database.child("user/\(userID)/abonniertePoi/").observe(.value, with: { snapshot in
for child in snapshot.children.allObjects as! [DataSnapshot] {
myFeed.myArray1.append(child.key)
}
print(myFeed.myArray1)
})
completion(arg)
}
It first prints second line executed then does the Firebase request and then says true.