I'm getting a
Error Domain=NSURLErrorDomain Code=-997 "Lost connection to background transfer service"
when I lock my screen after starting a background download process. I'm using Alamofire but I assume this would happen on a regular URLSession as well. The downloads continue to work if I soft close the app but as soon as I lock my device, it start throwing this error.
Here's the relevant part of code:
private(set) lazy var alamoSessionManager: SessionManager = {
let config = URLSessionConfiguration.background(withIdentifier: "MySession")
config.isDiscretionary = true
config.sessionSendsLaunchEvents = true
config.shouldUseExtendedBackgroundIdleMode = true
return Alamofire.SessionManager(configuration: config)
}()
let route = buildRouter()
alamoSessionManager.request(url, method: route.method, parameters: route.parameters, encoding: JSONEncoding.default, headers: route.headers)
I looked at the Console to see if any daemon crashed but couldn't find any. I see three errors right after locking the screen:
BKLogEvent: couldn't find CombinedSequence properties
Task <>.<48> finished with error - code: -999
Task <>.<48> load failed with error Error Domain=NSURLErrorDomain Code=-997 "Lost connection to background transfer service" UserInfo={NSErrorFailingURLStringKey=, NSErrorFailingURLKey=, _NSURLErrorRelatedURLSessionTaskErrorKey=, _NSURLErrorFailingURLSessionTaskErrorKey=, NSLocalizedDescription=Lost connection to background transfer service} [-997]
The error._userInfo is this:
▿ Optional<AnyObject>
▿ some : 5 elements
▿ 0 : 2 elements
- key : NSErrorFailingURLStringKey
- value : https://www.myweb.com/api
▿ 1 : 2 elements
- key : NSErrorFailingURLKey
- value : https://www.myweb.com/api
▿ 2 : 2 elements
- key : _NSURLErrorRelatedURLSessionTaskErrorKey
▿ value : 2 elements
- 0 : BackgroundDataTask <C39E-2FC73>.<27>
- 1 : LocalDataTask <C39E-2FC73>.<27>
▿ 3 : 2 elements
- key : _NSURLErrorFailingURLSessionTaskErrorKey
- value : BackgroundDataTask <C39E-2FC73>.<27>
▿ 4 : 2 elements
- key : NSLocalizedDescription
- value : Lost connection to background transfer service