iOS Swift: function not returning correct value
<p>I am developing an app in Swift for my high school sports teams. It gets the variable of what team you want to view when you click the button on the home view controller, and gets a link to a certain video based on the team selected. The problem occurs when I first select the team; it shows nothing, but when I click home and then the team again, it shows.</p>

<p>This is my code for the buttons in HomeViewController:</p>

<pre><code>import UIKit

class homeViewController: UIViewController {

@IBAction func varsityBasketballBtn(sender: AnyObject) {
    appDelegate.team = "VARSITY BASKETBALL"
}

@IBAction func varsityHockeyBtn(sender: AnyObject) {
    appDelegate.team = "VARSITY HOCKEY"
}

@IBAction func jvBasketballBtn(sender: AnyObject) {
    appDelegate.team = "JV BASKETBALL"
}

@IBAction func jvHockeyBtn(sender: AnyObject) {
    appDelegate.team = "JV HOCKEY"
}
</code></pre>

<p>After you click a button, it segues to the tabBarController, and sets the team variable of the AppDelegate to the team selected. In the AppDelegate, I created a function which retrieves the link of a video from parse.com based upon the team variable. I then put this function, which is supposed to return the embedLink, in my html code for my web view. when I first load the app and choose a team, the web view is blank, but after going home and re-selecting the team, it shows the correct video. I added print statements into my function which seem to show me that it is returning the embedLink variable before it runs my query from parse which seems to be my issue:    </p>

<pre><code>var embedLink:String = ""

func getLink() -&gt; String{

    let query = PFQuery(className:"LiveLink")
    print("This is right after query")
    query.findObjectsInBackgroundWithBlock{
        (liveLink:[PFObject]?,error: NSError?) -&gt; Void in
        print("This is before livelink")

        if error == nil &amp;&amp; liveLink != nil {
            for link in liveLink!{
                if link["team"] as! String == self.team{
                    self.embedLink = link["link"] as! String
                    print("This is link in query: \(self.embedLink)")
                }
            }
        } else {
            print(error)
        }
    }
    print("This is link in func: \(embedLink)")
    return embedLink
}
</code></pre>

<p>Please advise as to how you think this could be done correctly. Any help is appreciated.</p>