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!
swift2 alamofire
Groarus
source share