Reshad
Reshad

Reputation: 2652

Running a API call after another call is finished in swift 2.0

I have this issue where my app needs to make an api call for a some config values and then when this call is finished go do the actual call. In my method that fixes the calls I tried using a dispatch method but without success. anyone here who might have a clear solution of how to fix this? because right now I keep getting a nil value because of this..

func requestObj(url: Routes, params: Dictionary<String, String>?)
{
    let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
    dispatch_async(dispatch_get_global_queue(priority, 0)) {
        self.requestConfig() // request a valid config before doing anything
        dispatch_async(dispatch_get_main_queue(), {
            if let req = NSMutableURLRequest(urlString: self.config!.api!.baseUrl! + url.rawValue) {
                do {
                    req.addValue(String(self.config!.api!.token!), forHTTPHeaderField: "Token")
                    req.addValue(self.sessionID, forHTTPHeaderField: "SessionID")
                    let opt = HTTP(req)
                    opt.start { response in
                        if let err = response.error {
                            print("error: \(err.localizedDescription)")
                            return
                        }
                        print("opt finished: \(response.description)")
                        //print("data is: \(response.data)") access the response of the data with response.data
                    }

                }
            }
        })
    }
}

Upvotes: 0

Views: 852

Answers (2)

Jasper Fioole
Jasper Fioole

Reputation: 449

You could convert the following class to swift:

https://github.com/Shein/Categories/blob/master/NSURLConnection%2BTag.m

With this class you add a number to your request, for example 1. You could use an if statement to determine if it is the right request.

Upvotes: 0

rshev
rshev

Reputation: 4176

Add a completion handler with a closure to your requestConfig method and then perform operations in that completion handler.

Something like that, making completion handler optional:

func requestConfig(completionHandler completionHandler: (()->())?) {
    // do something and then
    completionHandler?()
}

func requestObj(url: Routes, params: Dictionary<String, String>?) {
    let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
    dispatch_async(dispatch_get_global_queue(priority, 0)) {
        self.requestConfig(completionHandler: { () -> () in
            dispatch_async(dispatch_get_main_queue(), {
                if let req = NSMutableURLRequest(urlString: self.config!.api!.baseUrl! + url.rawValue) {
                    do {
                        req.addValue(String(self.config!.api!.token!), forHTTPHeaderField: "Token")
                        req.addValue(self.sessionID, forHTTPHeaderField: "SessionID")
                        let opt = HTTP(req)
                        opt.start { response in
                            if let err = response.error {
                                print("error: \(err.localizedDescription)")
                                return
                            }
                            print("opt finished: \(response.description)")
                            //print("data is: \(response.data)") access the response of the data with response.data
                        }

                    }
                }
            })

        })
    }
}

Upvotes: 1

Related Questions