I'm trying to update a couple UILabels in my view, but I'm hitting a delay in the updating. I'm creating an NSURLSession, which I don't know if that's what's causing the problem, but I'm able to update other UILabels outside this chunk of code without any delay.
When this code gets executed, I see the two first printlns and their values immediately and also the third println with the string "done" also immediately in the output console. However in the simulator the activityIndicator runs and then about 15 seconds later the UILabels are updated and the activityIndicator stops. It's not an issue with the activityIndicator because even when I take that code out, there is still the delay with the UILabels updating. And I know it's not truly waiting for the data to be returned before updating the UILabels because the values are shown in the output console.
Any ideas on why there would be a delay in the updating/display in the view? And also why the console output seems to be jumping over the updating and printing the "done" string when in fact the lines of code right above it have yet to be completed?
@IBOutlet var attribute1: UILabel!
@IBOutlet var attribute2: UILabel!
...
var url = NSURL(string: "http://www.example.com")
if url != nil {
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: {
(data, response, error) -> Void in
var urlError = false
if error == nil {
var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding) as String!
// we then parse out the relevant bits of data
var urlContentArray = urlContent.componentsSeparatedByString("<header class=\"blah\")
if urlContentArray.count > 0 {
var attributesArray = urlContentArray[1].componentsSeparatedByString("<span class=\"something\">")
var att1Array = attributesArray[1].componentsSeparatedByString("</span>")
var att2Array = attributesArray[2].componentsSeparatedByString("</span>")
println(att1Array[0])
println(att2Array[0])
self.attribute1.text = att1Array[0]
self.attribute2.text = att2Array[0]
println("done")
self.activityIndicator.stopAnimating()
} else {
urlError = true
}
} else {
urlError = true
}
if urlError == true {
self.showURLError()
}
})
task.resume()
self.activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, screenSize.width, 930))
self.activityIndicator.hidesWhenStopped = true
self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.White
self.view.addSubview(self.activityIndicator)
self.activityIndicator.startAnimating()
} else {
showURLError()
}
0 comments:
Post a Comment