You can add an argument to the completion handler function in your getCurrentUser function and call the handler when retrieving data:
private func getCurrentUser(completion: (result: String) -> Void) { if PFUser.currentUser() != nil { currentUser = PFUser.currentUser()?.username let query = PFQuery(className: "_User") query.whereKey("username", equalTo: currentUser!) query.findObjectsInBackgroundWithBlock { (currentUsers, error) -> Void in if error == nil { for user in currentUsers! { completion(user["name"] as? String) } } } } }
Then pass the addition function as follows:
getCurrentUser() { (result: String) in self.lblUserName.text = result }
I can not prove that this is fully working code, since I do not have Xcode to test it. But you should get this idea.
source share