Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module 'Alamofire' has no member named 'SessionManager'

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()
                
            }
    }
    
}
like image 270
iPhone 7 Avatar asked Oct 23 '20 06:10

iPhone 7


1 Answers

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() }
}

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()
like image 179
gcharita Avatar answered Oct 10 '22 06:10

gcharita