Variable only updated in the sendAsynchronousRequest

161 views Asked by At

Example of what i wan't to do:

var b  = 0

let URL: NSURL = NSURL(string: "[URL to php file]")!
let request:NSMutableURLRequest = NSMutableURLRequest(URL: URL)
request.HTTPMethod = "POST"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
    (response, data, error) in
    b = 5
}

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return b //Return 0
}

I know that in numberOfRowsInSection it return 0 cause the sendAsynchronousRequest execute after but is there a way to make the numberOfRowsInSection function execute after the sendAsynchronousRequest ? Or is there a better way to make my variable equal 5 before numberOfRowsInSection function?

1

There are 1 answers

0
Nate Cook On BEST ANSWER

You don't want to delay the numberOfRows... call, but you can call tableView.reloadData once you've received the data in your asynchronous request:

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
    (response, data, error) in
    b = 5
    self.tableView.reloadData()
}

That will make the table view reconfigure itself and call numberOfRows... again.