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 (because internally Alamofire is using URLSession). The downloads continue to work if I soft close the app but as soon as I lock my device, it start throwing this error.
The URLSession is created this way:
let config = URLSessionConfiguration.background(withIdentifier: "MySession")
config.isDiscretionary = true
config.sessionSendsLaunchEvents = true
config.shouldUseExtendedBackgroundIdleMode = true
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:
▿ 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
I've about 300 of these background tasks. They will continue to work as long as I don't lock my device but as soon as I lock the device it starts throwing this error. My initial thought was that it was some sort of File protection errors but the userInfo from error doesn't point that way.