I am making an app in which I have to return a String representing the existence of a certain reference to my firebase realtime database (I am not using a Bool for other reasons). Despite executing the print commands directly above and below, when I print the function, the value of the String won't change and prints its default value "Unchanged".
My code:
func checkIfMovieHasAlreadyBeenShown(movieID: String) -> String {
var hasItBeenSeen = "Unchanged"
let uid = Auth.auth().currentUser?.uid
let ref = Database.database().reference()
ref.child("Seen Movies").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild(movieID){
print("The Result:")
self.changeStringHasItBeenSeen(YNString: "yes")
print("YES")
}
else {
print("The Result:")
self.changeStringHasItBeenSeen(YNString: "no")
print("NO")
}
})
return hasItBeenSeen
}
It is printed here:
print(checkIfMovieHasAlreadyBeenShown(movieID: "39"))
print(checkIfMovieHasAlreadyBeenShown(movieID: "38"))