IOS multithreading - NSURLSession and UI updates

I have a general question about multithreading in iOS:

In my very simple test application, I use NSURLSession to download small images from the server and present them in a table. In the NSURLSession callback, after retrieving the images, I call tableview.reloadData () as follows:

var session = NSURLSession.sharedSession().dataTaskWithURL(NSURL(url)) {(data, response, error) -> Void in
    /* Process images here */
    self.tableView.reloadData()
} session.resume()

Downloading images is almost instantaneous, but the table update is required to be displayed within 10-20 seconds! To be clear, update delegates do not even receive a call within 10-20 seconds. However, if I put reloadData () in the main thread, it will update quickly. For instance:

var session = NSURLSession.sharedSession().dataTaskWithURL(NSURL(url)) {(data, response, error) -> Void in
    /* Process images here */
    dispatch_async(dispatch_get_main_queue()) {
        self.tableView.reloadData()
    }
} session.resume()

, , . - , ? , - , FOREVER? , , 20 . , NSURLSession - ? ? ?

, .

+4
1
+2

All Articles