wait a task end for execute next code lines

i have a problem with my ios app, the problem is cause a part of my code download a “channel list”(json), and i need that this task end before execute the next line. im trying with dispatch_async but i havent good result because the next line allways is executed before the download task can ends.

class RemoteAPI {
    var remoteUrl = ""
    func getData(completionHandler: ((JSON!, NSError!) -> Void)!) -> Void {

    //let url: NSURL = NSURL(string: "https://api.cxntv.com/api/v1/videos/?type=canales&ordering=-id")
    let url: NSURL = NSURL(string: remoteUrl)!
    let request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
    let session = NSURLSession.sharedSession()
    //[mutableRequest addValue:@"x32dsds-223" forHTTPHeaderField:@"X_ROKU_ID"];

    //request.addValue("xxx", forHTTPHeaderField: "yyy")
    request.addValue("xxx" ,forHTTPHeaderField: "zzz")

    let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in

/* all my code for the httppetition ....*/

I call the getgadata function:

api.remoteUrl = "example"

    api.getData({data, error -> Void in
        if (data != nil){
            // Fix possible error if no "results" key
            if let results = data["results"].array {
                self.items = results
            dispatch_async(dispatch_get_main_queue()) {
            println("Data reloaded")
        } else {
            println("api.getData failed")

/*next line code for execute....

i need that this code lines wait that the getdata function ends for can be    executeded


thanks you

Source: ios

Leave a Reply