0

I am relatively new to working with Firebase and despite searching around cannot find the answer to my problem so was hoping someone could help.

I have a function that is designed to check if a user already exists within the databases "Users" collection. The function makes a call to where the user record would be and what I would like to happen is for it to return "true" if a record is found or "false" if one isn't.

However, I think (correct me if I'm wrong) the "getDocument" method hasn't finished running and therefore hasn't updated the variable when the return statement is read, therefore the function always returns false.

Does anyone have a way to ensure the "userStatus" variable can be updated before the function returns - or a different way of achieving the functions objective.

Thanks in advance!

       static func checkUserProfile(currentUserID:String) -> Bool {
  
        let userRecord = db.collection("Users").document(currentUserID)
        var userStatus = false
        
        userRecord.getDocument { docSnap, error in
            
            if error == nil {
                if docSnap != nil {
                    userStatus = true
                }
            
            }
        
    }
        return userStatus
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
stefds
  • 17
  • 6
  • 3
    for this type of function you need a completion handler instead of a return. The other way of doing it is with the provided async await methods – lorem ipsum Aug 24 '22 at 20:04
  • https://stackoverflow.com/questions/73429625/how-to-infer-a-generic-paramater-with-an-async-await-function/73430225#73430225 – lorem ipsum Aug 24 '22 at 20:06

0 Answers0