9

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()
                
            }
    }
    
}
gcharita
  • 7,729
  • 3
  • 20
  • 37
iPhone 7
  • 1,731
  • 1
  • 27
  • 63
  • You should not use `getAllTasks` to cancel tasks when using Alamofire. You should use the equivalent API from `Session`. – Jon Shier Oct 23 '20 at 17:26

1 Answers1

7

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()
gcharita
  • 7,729
  • 3
  • 20
  • 37
  • 2
    FYI, cancelling all requests in Alamofire should be done through `Session.cancelAllRequests` rather than on the tasks directly. – Jon Shier Oct 23 '20 at 17:31