First of all, I am just a beginner who is currently developing an app with the Swift language, so please don't mind my question too much because I really need to know and I am having trouble with maintaining the code that I constructed.
It's about the async delegate pattern.
Here is my API class. Assume that there are many API classes like that which makes async calls.
protocol InitiateAPIProtocol{
     func didSuccessInitiate(results:JSON)
     func didFailInitiate(err:NSError)
}
class InitiateAPI{
    var delegate : InitiateAPIProtocol
    init(delegate: InitiateAPIProtocol){
         self.delegate=delegate
    }
    func post(wsdlURL:String,action:String,soapMessage : String){
         let request = NSMutableURLRequest(URL: NSURL(string: wsdlURL)!)
         let msgLength  = String(soapMessage.characters.count)
         let data = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
         request.HTTPMethod = "POST"
         request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
         request.addValue(msgLength, forHTTPHeaderField: "Content-Length")
         request.addValue(action, forHTTPHeaderField: "SOAPAction")
         request.HTTPBody = data
         let task = session.dataTaskWithRequest(request) {
              data, response, error in
            if error != nil {
                 self.delegate.didFailInitiate(error!)
                 return
            }
            let jsonData = JSON(data: data)
            self.delegate.didSuccessInitiate(jsonData)
        }
        task.resume()
}
func doInitiate(token : String){
    let soapMessage = “”
// WSDL_URL is the main wsdl url i will request.
action = “”
    post(WSDL_URL, action: action, soapMessage: soapMessage)
}
}
Here is my ViewController:
class ViewController : UIViewController,InitiateAPIProtocol{
    var initiateAPI : InitiateAPI!
    var token : String = “sWAFF1”
    override func viewWillAppear(animated: Bool) {
            // Async call start
            initiateAPI = InitiateAPI(delegate:self)
            initiateAPI.doInitiate(token)
    }
    // Here comes call back
    func didSuccessInitiate(results: JSON) {
       //handle results
    }
    func didFailInitiate(err: NSError) {
       //handle errors
    }
}
My problem is I said that there are many API classes like that, so if one view controller handles 4 API classes, I have to handle many protocol delegates methods as I extend the view controller. There will be many delegates method below of view controller. If other view controllers call the same API and have to handle the same delegates, I have a problem maintaining the code because every time I change some delegate parameters, I have to fix the code at all view controllers which use those API classes.
Is there any other good way to handle async call?
If my question seems a little complex, please leave a comment, I will reply and explain it clearly.
 
     
     
     
     
    