title: iOS takes long time to update view after HTTP request?
body: <p>I am very new to learning iOS and Swift, so there may be something very basic here that I don't understand. I am using the <a href="https://github.com/hallas/agent" rel="nofollow" title="agent">agent</a> library to send HTTP requests in Swift. Right now I'm just getting the hang of creating the request and parsing the JSON response.</p>
  
  <p>I have a very simple UI with a button and a <code>UILabel</code> that I want to update with the results of some JSON. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>func updateWithJson() -&gt; Void {
      let req = Agent.get("http://xkcd.com/info.0.json")
      req.end({ (response: NSHTTPURLResponse!, data: Agent.Data!, error: NSError!) -&gt; Void in
          let json = data! as Dictionary&lt;NSString, NSString&gt;
          // this takes almost 30 seconds to propagate to the UI...
          self.updateMe.text = json["safe_title"]!
      })
  }
  </code></pre>
  
  <p>The part I don't understand is the <code>self.updateMe.text = json["safe_title"]!</code> statement which is just updating some text on a <code>UILabel</code> called <code>updateMe</code>. It takes almost a full 30 seconds for this to reflect in the UI.</p>
  
  <p>I know that the HTTP request itself is very fast - if I curl that URL it comes back instantaneously. It also seems like the <code>Agent</code> object is making the connection and returning the response pretty fast.</p>
  
  <p>Am I missing something very basic? Should I be updating the UI in a different way? All pointers are appreciated!</p>
  
  <p><strong>UPDATE</strong>
  Through another SO post it's my understanding that the block inside of <code>req.end</code> is a background thread and I need to do UI updates on the main thread. I updated my function to look like this and I get the desired result. However, now I'm wondering if there's something incorrect about doing it this way?</p>
  
  <pre><code>    func updateWithJson() -&gt; Void {
          let req = Agent.get("http://xkcd.com/info.0.json")
          req.end({ (response: NSHTTPURLResponse!, data: Agent.Data!, error: NSError!) -&gt; Void in
              let json = data! as Dictionary&lt;NSString, NSString&gt;
              var text = json["safe_title"]!
              dispatch_sync(dispatch_get_main_queue()) {
                  self.updateMe.text = text
              }
          })
      }
  </code></pre>