Hi there,
I'm struggling with creating a request which continues to run even if the app enters background, however I keep getting the code mentioned in the title, I try to make the request as followed:
let configuration = URLSessionConfiguration.background(withIdentifier: "com.company.myapp.background")
let manager = Alamofire.SessionManager(configuration: configuration)
manager.request(url, method: .get, parameters: params, headers: headers())
.responseJSON{response in switch response.result {
...
The error follows:
Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=http://url, NSLocalizedDescription=cancelled, NSErrorFailingURLKey=http://url?params}
What am I doing wrong? I've stumbled over a couple of recent questions on stackoverflow, but they didn't seem resolved. Is my approach towards letting the request continue with the app in the background even correct?
Feel free to close this if it's encountered as support, I just feel I'm quite lost.
Cheers, and thanks.
An unexpected error -999 almost always means your SessionManager was deallocated, cancelling any ongoing requests. I suggest you create a singleton value for your custom SessionManager, or perhaps just reevaluate if you really need one.
Best Practice
open class API_Manager : Alamofire.SessionManager {
public static let sharedManager: Functions = {
let configuration = URLSessionConfiguration.default // or your configuration
configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders // or your headers
configuration.timeoutIntervalForRequest = 30
configuration.timeoutIntervalForResource = 30
let manager = Functions(configuration: configuration, delegate: SessionManager.default.delegate)
return manager
}()
override init(configuration: URLSessionConfiguration, delegate: SessionDelegate, serverTrustPolicyManager: ServerTrustPolicyManager? = nil) {
super.init(configuration: configuration, delegate: delegate, serverTrustPolicyManager: serverTrustPolicyManager)
}
required public init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
After that
API_Manager.sharedManager.request(url, method: .get, parameters: params, headers: headers())
.responseJSON{response in switch response.result {
}
I would recommend not subclassing unless you need behavior that can't be accomplished using the public APIs. Also, following Swift naming conventions. But yes, a singleton in a type is generally how you'd use a custom SessionManager.
@cnoon Perhaps we need a best practices or common issues section in the README. This, and other issues, come up fairly often.
@jshier Agree.
More proper will be just like this
public static let sharedManager: SessionManager = {
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
configuration.timeoutIntervalForRequest = 40
configuration.timeoutIntervalForResource = 40
let manager = Alamofire.SessionManager(configuration: configuration)
return manager
}()
Subclass of SessionManager is needed from my side but in General not recommended or not required.
Most helpful comment
@jshier Agree.
More proper will be just like this
Subclass of
SessionManageris needed from my side but in General not recommended or not required.