本文介绍了模块“Alamofire"没有名为“SessionManager"的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我会收到此错误,我没有.我已经在使用Alamofire 5时将Alamofire更改为AF.请指导有什么问题以及需要更改的内容.以下是我遇到错误的代码:

Why I am getting this error, I didn't got. I already changed Alamofire to AF as using Alamofire 5. Please guide what's wrong and what need to change. Below is my code where i am getting error:

 private func callAlamoFireFormalData(makeThisFunction : @escaping (AFDataResponse<Any>)->Void )
{
    AF.request(url, method: httpMethod, parameters: paramters, encoding: encoding, headers: HTTPHeaders(self.headers ?? [:]))
        //.validate(statusCode: 200..<300)
        .responseJSON { response in

            //print("\(response.response?.statusCode)")
            guard response.response?.statusCode != 401
                else{

                    Alamofire.SessionManager.default.session.getAllTasks { tasks in
                        tasks.forEach { $0.cancel() }
                    }
                    Helper.setApiToken(token: "")
                    Helper.removeUserInfo()
                    if var topController = UIApplication.shared.keyWindow?.rootViewController {
                        if let presentedViewController = topController.presentedViewController
                        {
                            topController = presentedViewController
                            topController.present(UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "InitialNAV"), animated: true, completion: nil)
                        }

                        // topController should now be your topmost view controller
                    }
                    //                        Alamofire.pause()
                    return
            }

            switch response.result
            {
            case let .failure(error):

                print(error)
                self.serverResponse.error = true
                self.serverResponse.message = String.serverError
                self.serverResponse.content = nil
                self.serverResponse.statusCode = response.response?.statusCode
                makeThisFunction()

            case .success(let value):

                let json = JSON(value)
                self.serverResponse.error = json["error"].boolValue
                self.serverResponse.message = json["message"].stringValue
                self.serverResponse.content = json["content"]
                self.serverResponse.statusCode = response.response?.statusCode
                makeThisFunction()

            }
    }

}

推荐答案

SessionManager 类在 Alamofire 5 中基本上重命名为 Session.所以你可以用这个替换你的实现:

SessionManager class essentially renamed to just Session in Alamofire 5. So you can replace your implementation with this:

Alamofire.Session.default.session.getAllTasks { tasks in
    tasks.forEach { $0.cancel() }
}

更新:正如@JonShier 在评论中提到的,取消所有请求的正确方法是调用 SessioncancelAllRequests(completingOnQueue:completion:) 功能:

Update: As @JonShier mentioned in the comment, the correct way to cancel all requests is by calling Session's cancelAllRequests(completingOnQueue:completion:) function:

Alamofire.Session.default.cancelAllRequests()

这篇关于模块“Alamofire"没有名为“SessionManager"的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 23:20