I've tried to understand this by reading: https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/
But it is soooooo confusing. I know that the following code has to do something with queueing the task. I think that queueing means waiting? And I'm not sure what the task is. And I know that it somehow speeds up the execution of the code that is inside this code. Still, I'm very confused when it is used and why it is used.
dispatch_async(dispatch_get_main_queue(), { () -> Void in
})
The above code is found in this body of code:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var cityTextField: UITextField!
@IBOutlet weak var resultLabel: UILabel!
@IBAction func findWeather(sender: AnyObject) {
let url = NSURL(string: "http://www.weather-forecast.com/locations/Riverside/forecasts/latest")!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
if let urlContent = data {
let webContent = NSString(data: urlContent, encoding: NSUTF8StringEncoding)
var websiteArray = webContent!.componentsSeparatedByString("3 Day Weather Forecast Summary:</b><span class=\"read-more-small\"><span class=\"read-more-content\"> <span class=\"phrase\">")
let tempText = websiteArray[1]
websiteArray = tempText.componentsSeparatedByString("</span></span></span></p><div class=\"forecast-cont\"><div class=\"units-cont\"><a class=\"units metric active\">°C</a><a class=\"units imperial\">°")
print(websiteArray[0])
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.resultLabel.text = websiteArray[0]
})
}
}
task?.resume()
}