I have an issue which should not be an issue in my opinion.
When I load this 'simple' function, it should return the users within the DB.
- returns usersDictionary: All the users in a dictionary
*/
func getAllUsers() -> Dictionary<String, AnyObject> {
var usersDict = Dictionary<String, AnyObject>()
if userIsLoggedIn() == true{
FIRDatabase.database().reference().child("users").observeSingleEvent(of: .value, with: { (snapshot) in
if let userDictionary = snapshot.value as? [String:AnyObject]{
for user in userDictionary{
usersDict.updateValue(user.value as AnyObject
, forKey: user.key as String)
}
print("TEST")
print(usersDict)
}
print("TEST2")
print(usersDict)
})
print("TEST3")
print(usersDict)
return usersDict
}
return usersDict
}
However it does not return usersDict with value, but just empty which is strange because it prints out the value of TEST and TEST2 usersDict.
But Test3 remains empty, how is this possible? If I look in the Console, it does Test3 first and then test and then test2.
This is what console prints:
TEST3
[:]
TEST
"ZuUiTSu142P5NTc9VfekRbuFcny2": {
address = "testAddress";
email = "test@outlook.com";
name = "testuser";
phonenumber = "1234567890";
rol = Customer;
}]
TEST2
"ZuUiTSu142P5NTc9VfekRbuFcny2": {
address = " testAddress ";
email = " test@outlook.com ";
name = "testuser";
phonenumber = "1234567890";
rol = Customer;
}]