Ricardo42
Ricardo42

Reputation: 103

Completion Handler called too late

I'm trying to fill a UITableView with data that I acquire via a DataTaskWithURL call. However, the tableview has loaded before the completion handler has started.

I searched for this problem and found things about moving it to the main thread but that didn't seemed to work. Any idea on how I can move it to the right que before the tableview had loaded?

My code:

    var jsonData: JSON = []

func getRequestForTableView() {

    let urlPath = "[URL]"
    let url: NSURL = NSURL(string: urlPath)!
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in

        let json = JSON(data: data)

        println(json.count)
        self.jsonData = json
    })
    task.resume()
}

Upvotes: 0

Views: 400

Answers (1)

Chetan Prajapati
Chetan Prajapati

Reputation: 2297

Needs to Check response code and Your Data first. Response code like 200. If there are thing you are looking for, You need to Reload TableView in your block after confirmation of your response.

Make sure you correctly interpret your data in TableView.

Reload TableView // Swift

dispatch_async(dispatch_get_main_queue(), { () -> Void in
    self.tableView.reloadData()
})

Response Code // iOS

if ([response statusCode] == 200) {
           //reload table here             
}

Upvotes: 1

Related Questions