I'm having a RegisterManager like this:
class RegisterManager {
private let manager : Alamofire.Manager
init(){
let configuration = Timberjack.defaultSessionConfiguration()
manager = Alamofire.Manager(configuration: configuration)
}
}
Then I have some methods on RegisterManager for example:
func loginUser(username: String, password: String, completionHandler: CompletionHandler) {
let parameters: [String:String] = [
"username": username,
"password": password,
"scope": "user_basic",
"grant_type": "password"
]
let headers = [
"Authorization": "Basic \(getBase64Credentials())",
"Content-Type": "application/x-www-form-urlencoded"
]
manager.request(.POST, String(format: "%@%@", arguments: [kAPIHost, "/oauth/token"]), parameters: parameters, headers: headers).responseJSON {
response in
if let _ = response.result.value {
completionHandler(success: true)
} else {
completionHandler(success: false)
}
}
}
When I do the call it always fails because it's cancelled. When I just change manager.request to Alamofire.request it works. I found here on the github that you need to have a let in your class so I did but still the same problem.
What I'm doing wrong?
@donpironet I believe you need _.sharedInstance_ on the end of your _manager_ constant.
But then I have a instance again with the default configuration?
@donpironet You'll need to keep a reference to your manager around. You requests are being cancelled because the manager you're using is being deallocated. I like to use a static let sharedManager in my Manager subclass to maintain a single instance.
But I have a private let variable in my RegisterManager? So why is that not working?
Because it's not static. A private let is just an instance variable, one that's deallocated with the instance. Create a static let sharedManager = RegisterManager() and use it for your request calls (i.e. RegisterManager.sharedManager.request()).
I found the problem. It was indeed in the class that creates my RegisterManager. I didn't save a reference to it. Feeling so stupid.
Most helpful comment
@donpironet You'll need to keep a reference to your manager around. You requests are being cancelled because the manager you're using is being deallocated. I like to use a
static let sharedManagerin myManagersubclass to maintain a single instance.