func callData() -> String? {
var name: String = ""
var ref: DatabaseReference!
ref = Database.database().reference()
ref.child("reyddatbase").child("MovieData").child("InitialMovieData").child("Ariel").child("movie_name").observeSingleEvent(of: .value, with: { (snapshot) in
name = ((snapshot.value) as! NSString) as String;
print("\(name)") // prints what I need
})
print("\(name)") // prints nothing??
return "\(name)" // returns nothing?
}
I am attempting to read data from a database based in Firebase. I would like to simply display the text on screen which I can do from content view.
However the print statement inside the ref.child
shows my title in console, while my print statement outside of it remains an empty string.
I am not sure why the different print statements do not yield the same result.
I am very new to swift and have taught myself to this point. I presume there is a simple solution to my problem. Thank you in advance for the help!