Pass method to run on NSURLConnection's connectionDidFinishLoading

109 views Asked by At

I'm creating an app where i call my API through this function:

func serverCallAPI(function:String, 
                   success:((result:NSDictionary) -> Void)?) -> Void {
    //code here to set up request to send with NSURLConnection
    let connection:NSURLConnection = NSURLConnection(request: request, 
                                                     delegate: self, 
                                                     startImmediately: true)!

    //I WANT TO RUN THE PASSED ON SUCCESS FUNCTION WHEN connectionDidFinishLoading IS CALLED.
}
  1. I need to use NSURLConnection to update my UI when fetching the data
  2. I might have a queue with serverCallAPIs waiting in line to be run, every single now with their own success function. The final function needs to be able to do that
  3. It needs to cancel if the connection fails. Perhaps something with a dispatch or NSOperation? I'm lost on this one... :(

My problem is that I cannot think of a way to call the passed on success function when connectionDidFinishLoading is done processing. How can you do that?

0

There are 0 answers