I am using Firebase to store data, and when I try to retrieve data using their closures functions always finish and return before the closure finishes. In my HeyUser class i am trying to get the values in the Friend key in my Firebase and store it in a class property. However when trying to instantiate the object of the class the object is instantiated without getting the values of the Friends key. I was wondering if you are able to delay the instantiation or the completion of a function until the closure is completed. The following is the code of my HeyUser class:
class HeyUser {
var Friends: [String] = [String]()
var username = PFUser.currentUser().username
var ref = Firebase()
var fRef = Firebase()
init() {
self.ref = Firebase(url: "https://hey-chat.firebaseio.com/").childByAppendingPath("users").childByAppendingPath(PFUser.currentUser().username)
self.fRef = self.ref.childByAppendingPath("friends")
self.getFriends()
}
func getFriends() {
self.fRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
self.Friends = snapshot.value as [String]
print("TEST")
print(self.Friends.count)
})
}
}