Alamofire响应处理程序未调用

时间:2017-08-10 10:56:22

标签: ios swift networking alamofire

我希望有人可以帮助我。

当我执行请求时,401来自服务器,我刷新身份验证令牌。如果刷新请求成功,那么我再次执行请求,但这次没有调用响应处理程序。

我尝试在并发队列上执行响应处理,但这没有帮助。

有人知道这个问题吗?

fileprivate func request(WithRequestType requestType: RequestType, parameters: [String : Any]?, controller: UIViewController?, success: @escaping ([String: Any]?) -> Void, error: (() -> Void)?) {
    let urlRequest = buildURLRequest(WithRequestType: requestType, parameters: parameters)

    Alamofire.request(urlRequest).responseJSON { response in
        switch response.result {
        case .success(let JSON as [String: Any]):
            success(JSON)
        case .failure(let ERROR):

            // MARK: - FAILURE

                LoggingHelper.log(errorForRequest: response.request!, error: ERROR)
                if let statusCode = response.response?.statusCode {
                    if statusCode == 401 {
                        //auth token is invalid and need to be refreshed
                        self.refresh(WithRequestType: requestType, parameters: parameters, controller: controller, success: success, error: error)
                        return
                    }
                }

                if controller != nil {
                    LoggingHelper.showNetworkError(ForViewController: controller!, andError: ERROR)
                }

                error?()
        default:

            // MARK: - SUCCESS NO CONTENT

            let statusCode = (response.response?.statusCode)! as Int
            if statusCode == 204 {
               success(nil)
            }
        }

和刷新方法,再次调用请求

fileprivate func refresh(WithRequestType requestType: RequestType, parameters: [String : Any]?, controller: UIViewController?, success: @escaping ([String: Any]?) -> Void, error: (() -> Void)?) {
    guard let refreshToken = PersistDataHelper.loadAuthentication()?.refreshToken else {
        LoggingHelper.log(errorForClass: RequestHelper.self, with: "No refresh token")
        self.register(WithRequestType: requestType, parameters: parameters, controller: controller, success: success, error: error)
        return
    }

    let encryptedRefreshToken = AuthenticationHelper.encrypt(refreshToken, forKey: REFRESH_TOKEN)
    let parameters: [String: Any] = [TOKEN: encryptedRefreshToken]
    let urlRequest = buildURLRequest(WithRequestType: .refresh, parameters: parameters)

    Alamofire.request(urlRequest).responseJSON { response in
        switch response.result {
        case .success(let JSON as [String: Any]):
            self.request(WithRequestType: requestType, parameters: parameters, controller: controller, success: success, error: error)
            break
        case .failure(let ERROR):

            // MARK: - FAILURE

            //DispatchQueue.main.async {
                LoggingHelper.log(errorForRequest: response.request!, error: ERROR)
                if let statusCode = response.response?.statusCode {
                    if statusCode == 401 {
                        //refreshtoken is invalid and need to be refreshed
                        self.register(WithRequestType: requestType, parameters: parameters, controller: controller, success: success, error: error)
                        return
                    } else {
                        if controller != nil {
                            LoggingHelper.showNetworkError(ForViewController: controller!, andError: ERROR)
                        }
                    }
                }
                error?()
            break
            //}
        default:
           error?()
        }

0 个答案:

没有答案