I have written a function that makes a JSON request and puts the result in three arrays.
I want to present a view controller after this JSON request is completed fully.
I tried putting the action below the for-in loop, but this didn't work.
This is my code:
func getJSON() {
var vc = self.storyboard?.instantiateViewControllerWithIdentifier("myVCId") as ViewController
let urlAsString = "http://localhost:8888/domainchecker/check.php?domain=/" + tfDomain.text;
println(urlAsString);
let url = NSURL(string: urlAsString)!
let urlSession = NSURLSession.sharedSession()
let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
if (error != nil) {
println(error.localizedDescription)
}
var err: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
if (err != nil) {
println("JSON Error \(err!.localizedDescription)")
}
let arrayFromJson: [[String:String]] = jsonResult["ITEMS"] as [[String:String]]
dispatch_async(dispatch_get_main_queue(), {
for item in arrayFromJson {
vc.tableData.append(item["DOMAIN"]!);
vc.tablePrice.append(item["PRICE"]!);
vc.tableAvailability.append(item["AVAILABLE"]!);
}
self.presentViewController(vc, animated: true, completion: nil)
})
})
jsonQuery.resume()
}
This is the action I want to do after the code has completed:
self.presentViewController(vc, animated: true, completion: nil)
if (arrayFromJson.count > 0) {
self.presentViewController(vc, animated: true, completion: nil)
}
this is it.