HixField
HixField

Reputation: 3776

Alamofire custom reponse migration from Alamofire v1.3 to 3.0 (and Swift 2 syntax)

I am converting an project from Swift 1.1 to Swift 2.0 that used Alamofire 1.3. Because this version of Alamofire was not compatible with Swift 2.0, I switched to the very last version of Alamofire (3.1.x).

However this broke the code, I am now getting the error message

"Cannot call value of non-function type 'NSHTTPURLResponse?'"

which is due to my my extension to create a custom "responseShop" Alamofire.Request.

How can this Alamofire.Request extension correctly be converted to the Alamofire 3.x/Swift 2.0 syntax?

extension Alamofire.Request {
    func responseShop(options: NSJSONReadingOptions = .AllowFragments, var errorHandler:ProtocolWebServiceErrorHandler?, completionHandler: (ShopCallResult?, WebServiceErrorCode) -> Void ) -> Self {
        objc_sync_enter(communicationLockObj)
        return response(serializer: Request.JSONResponseSerializer(options: options), completionHandler: {
            (request, response, JSON, error) in
            log.verbose("\(JSON)")
            //network error?
            if let error = error {
                //is it cancelled?
                if error.code == -999 {
                    dispatch_async(dispatch_get_main_queue()) {
                        //call handler with cancelled code
                        completionHandler(nil, WebServiceErrorCode.requestCancelled)
                    }
                    objc_sync_exit(communicationLockObj)
                    return
                }

                dispatch_async(dispatch_get_main_queue()) {
                    errorHandler?.handleWebServiceError(WebServiceErrorCode.connectError, errorText : error.localizedDescription, request: request, json: JSON)
                    //call and supply org error
                    completionHandler(nil, WebServiceErrorCode.connectError)
                }
                objc_sync_exit(communicationLockObj)
                return
            }
            if JSON == nil {
                dispatch_async(dispatch_get_main_queue()) {
                    errorHandler?.handleWebServiceError(WebServiceErrorCode.jsonNil, errorText: nil, request: request, json: JSON)
                    //call and supply org error
                    completionHandler(nil, WebServiceErrorCode.jsonNil)
                }
                objc_sync_exit(communicationLockObj)
                return
            }
            //api return error?
            let callResult = ShopCallResult(json: JSON!)
            if callResult.statusCode.failed {
                dispatch_async(dispatch_get_main_queue()) {
                    errorHandler?.handleWebServiceError(callResult.statusCode, errorText: callResult.statusCode.localizedText, request: request, json: JSON)
                    completionHandler(callResult, callResult.statusCode)
                }
                objc_sync_exit(communicationLockObj)
                return
            }
            //no error
            dispatch_async(dispatch_get_main_queue()) {
                completionHandler(callResult, WebServiceErrorCode.OK)
            }
            objc_sync_exit(communicationLockObj)
        })
        return self
    }
}

Upvotes: 1

Views: 147

Answers (1)

HixField
HixField

Reputation: 3776

Based on the feedback of @ProblemSolver I managed to convert the code, now it looks like this:

extension Alamofire.Request {

    func responseShop(queue: dispatch_queue_t? = nil, options: NSJSONReadingOptions = .AllowFragments, errorHandler:ProtocolWebServiceErrorHandler?, completionHandler: (ShopCallResult?, WebServiceErrorCode) -> Void ) -> Self {
        //enter thread protected area...
        objc_sync_enter(communicationLockObj)

        return responseJSON() {
            response in
            switch response.result {
            case .Success(let JSON):
                //log json in verbose mode
                log.verbose("\(JSON)")
                //parse the returned json
                let callResult = ShopCallResult(json: JSON)
                //is it failed?
                if callResult.statusCode.failed {
                    //call supplied error handler on the main thread
                    dispatch_async(dispatch_get_main_queue()) {
                        errorHandler?.handleWebServiceError(callResult.statusCode, errorText: callResult.statusCode.localizedText, request: self.request!, json: JSON)
                        completionHandler(callResult, callResult.statusCode)
                    }
                    //release the lock
                    objc_sync_exit(communicationLockObj)
                    //processing done!
                    return
                }
                //no error call handler on main thread
                dispatch_async(dispatch_get_main_queue()) {
                    completionHandler(callResult, WebServiceErrorCode.OK)
                }
                //release the lock
                objc_sync_exit(communicationLockObj)

            case .Failure(let error):
                //WARNING: cancelled is still error code 999?
                //is it cancelled?
                if error.code == -999 {
                    //just call the completion handler
                    dispatch_async(dispatch_get_main_queue()) {
                        //call handler with cancelled code
                        completionHandler(nil, WebServiceErrorCode.requestCancelled)
                    }
                    //release the lock
                    objc_sync_exit(communicationLockObj)
                    //stop furhter processing
                    return
                }
                //error with the json?
                if error.code == Alamofire.Error.Code.JSONSerializationFailed .rawValue {
                    //call the error handler
                    dispatch_async(dispatch_get_main_queue()) {
                        errorHandler?.handleWebServiceError(WebServiceErrorCode.jsonNil, errorText: nil, request: self.request!, json: nil)
                        //call and supply org error
                        completionHandler(nil, WebServiceErrorCode.jsonNil)
                    }
                    //release the lock
                    objc_sync_exit(communicationLockObj)
                    //stop further processing
                    return
                }
                //must be some other kind of network error
                dispatch_async(dispatch_get_main_queue()) {
                    log.error("\(error)")
                    //so call the error handler
                    errorHandler?.handleWebServiceError(WebServiceErrorCode.connectError, errorText : error.localizedDescription, request: self.request!, json: nil)
                    //call and supply org error
                    completionHandler(nil, WebServiceErrorCode.connectError)
                }
                //release the lock
                objc_sync_exit(communicationLockObj)
            }
        }
    }
}

Upvotes: 1

Related Questions