Can anyone tell me how to use the Alamofire background service correctly?
My attempts are:
Login View Controller
// Create a global variable for the manager
var manager = Alamofire.Manager()
var configuration = NSURLSessionConfiguration()
class LoginViewController: UIViewController {
// set the configuration for the manager
configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.xxx.app.backgroundService")
configuration.sharedContainerIdentifier = "com.xxx.app.backgroundService"
}
Now creating my login attempt, when successful go to the MainViewController and do here some more Requests.
manager.request(.POST, "\(serviceUrl)public/service/authenticate", parameters: ["email": txtEmail.text!, "password": txtPassword.text!]) {
......
}
Here ill get my token for all requests, so ill add the to my global configuration:
class MainViewController: UIViewController {
if let token: AnyObject = NSUserDefaults.standardUserDefaults().objectForKey("token") {
configuration.HTTPAdditionalHeaders = ["Authorization": "Bearer \(token)"]
manager = Alamofire.Manager(configuration: configuration)
}
}
But now when ill logout and login again - ill get the following errors:
Warning: A background URLSession with identifier xxx.xxx.app.backgroundService already exists!
And the app crashes with:
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Task created in a session that has been invalidated'
*** First throw call stack:
So i really dont know how do use the background-service to get the following result:
At login attempt, i dont need to add an authentication header. But then, i would like to add it to every request. All requests should run as background service.
Can anyone help me out, how to solve that in a correct way? Thanks in advance!