Alamofire manager with Swift 2.0 gets canceled

I have a Swif 2.0 ApiManager class using Alamofire 2.0 with the following init:

var manager:Manager init() { var defaultHeaders = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ?? [:] defaultHeaders["Authorization"] = "Bearer \(UserAccount.sharedInstance.token)" let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.HTTPAdditionalHeaders = defaultHeaders manager = Alamofire.Manager(configuration: configuration) } 

Function example:

 func getMe(completion:(jsonObject: NSDictionary) -> ()) { manager.request(.GET, Constants.apiURL + "me").responseJSON { request, response, result in print(self.manager) //THIS LINE FIXES IT switch result { case .Success(let json): completion(jsonObject: json as! NSDictionary) case .Failure(let data, let error): print("Error: \(__FUNCTION__)\n", data, error) } } } 

The error I get is:

 Error Domain=NSURLErrorDomain Code=-999 "cancelled" 

It looks like the request is being canceled because the dispatcher is being freed. Adding a print statement prevents the release of the manager, and then it works great. But I'm trying to find a better solution.

Any help would be appreciated!

+7
swift2 alamofire
source share
2 answers

I worked with the same problem, I used this to fix it

 class Session { static let sharedInstance = Session() private var manager : Manager? func ApiManager()->Manager{ if let m = self.manager{ return m }else{ let defaultHeaders = ["X-Version":"Some header"] let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.HTTPAdditionalHeaders = defaultHeaders let tempmanager = Alamofire.Manager(configuration: configuration) self.manager = tempmanager return self.manager! } } } 

then i called him

  Session.sharedInstance.ApiManager().request(... 
+13
source share

It is resolved here in its github issues https://github.com/Alamofire/Alamofire/issues/157

+1
source share

All Articles