Swift UITableView reloadData in a closure

I believe I’m having an issue where my closure is happening on a background thread and my UITableView isn’t updating fast enough. I am making a call to a REST service and in my closure i have a tableView.reloadData() call but it takes a few seconds for this to happen. How do I make the data reload faster (perhaps on the main thread?)

REST Query Function – using SwiftyJSON library for Decoding

  • Warning: Attempt to present ViewController on ViewController whose view is not in the window hierarchy
  • Importing pure Swift custom framework into other Swift project
  • What is the difference between -viewWillAppear: and -viewDidAppear:?
  • iOS memory allocation - how much memory can be used in an application?
  • how to add xib file in static library in iOS
  • How to make “shaking” animation
  • func asyncFlightsQuery() {
        var url : String = "http://127.0.0.1:5000/flights"
        var request : NSMutableURLRequest = NSMutableURLRequest()
        request.URL = NSURL(string: url)
        request.HTTPMethod = "GET"
    
        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, networkData: NSData!, error: NSError!) -> Void in
            var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
    
    
            // Parse with SwiftyJSON
            let json = JSON(data: networkData)
    
            // Empty out Results array
            self.resultArray = []
    
            // Populate Results Array
            for (key: String, subJson: JSON) in json["flights"] {
                print ("KEY: \(key) ")
                print (subJson["flightId"])
                print ("\n")
    
                self.resultArray.append(subJson)
            }
    
            print ("Calling reloadData on table..??")
            self.tableView.reloadData()
    
    
        })
    }
    

    Once self.tableView.reloadData() is called in my debugger

    5 Solutions Collect From Internet About “Swift UITableView reloadData in a closure”

    UIKit isn’t thread safe. The UI should only be updated from main thread:

    dispatch_async(dispatch_get_main_queue()) {
        self.tableView.reloadData()
    }
    

    Update. In Swift 3 use:

    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
    

    You can also reload UITableView like this

    self.tblMainTable.performSelectorOnMainThread(Selector("reloadData"), withObject: nil, waitUntilDone: true)
    

    With Swift 3 use

    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
    

    SWIFT 3:

    OperationQueue.main.addOperation ({
         self.tableView.reloadData()
    })
    

    You can also update the main thread using NSOperationQueue.mainQueue(). For multithreading, NSOperationQueue is a great tool.

    One way it could be written:

    NSOperationQueue.mainQueue().addOperationWithBlock({
         self.tableView.reloadData()       
    })
    

    Update: DispatchQueue is the way to go for this