Swift: tableView.reloadData() way too slow

2.2k views Asked by At

I'm trying to execute a tableView.reloadData() call after an async request that I'm calling to populate my UITableView. The table displays the data after 15-20 seconds, unless I manually start scrolling, in which case it all loads immediately. So the data is definitely there, it's just not being loaded properly. What can I do to resolve this?

var jsonLoaded:Bool = false {
        didSet {
            if jsonLoaded {

                tableView.reloadData()

            }
        }
    }

override func viewDidLoad() {
        super.viewDidLoad()

let url = NSURL(string: "https://www.googleapis.com/youtube/v3/search?part=id&q=\(searchTerm)&maxResults=1&key=AIzaSyD7PxAoq0O5hUxx775l_E_mnowlU4cUfcI")

            let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in

                if error != nil {

                    println(error)

                } else {

                    let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary

                    if let items = jsonResult["items"] as? NSArray {
                        for songs in items {
                            if let id = songs["id"] as? NSDictionary {
                                if let videoId = id["videoId"] as? String {
let newURL = NSURL(string: "https://www.googleapis.com/youtube/v3/search?relatedToVideoId=\(videoId)&part=snippet&type=video&maxResults=6&key=AIzaSyD7PxAoq0O5hUxx775l_E_mnowlU4cUfcI")

                                    let newTask = NSURLSession.sharedSession().dataTaskWithURL(newURL!, completionHandler: {data, response, error -> Void in

                                        if error != nil {

                                            println(error)

                                        } else {

                                            let newJsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
                                            if let info = newJsonResult["items"] as? NSArray {
                                                for videos in info {
                                                    if let snippet = videos["snippet"] as? NSDictionary {
                                                        if let title = snippet["title"] as? String {
                                                           self.recommendedTitles.append(title)
                                                           self.jsonLoaded = true

                                                     }
                                                }
                                           } 
                                      }
                                  })
                                  newTask.resume()
                              }
                          }
                     }
                 jsonLoaded = false
             }
          }
      }
  })
}
}
1

There are 1 answers

1
matt On BEST ANSWER

The problem is that you are setting jsonLoaded on a background thread. This means that reloadData() is called on a background thread as well. But you must never talk to the interface on a background thread! You need to step out to the main thread before doing anything that involves shared data (such as jsonLoaded) or the interface.