URLSessionDownloadTask fails with error -997 as soon as I lock my device.

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.

Replies

Have you tried this without

shouldUseExtendedBackgroundIdleMode
? That flag is definitely unnecessary for background sessions. Setting it should be a no-op, but I’d like to confirm that before moving on.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi


Thanks for the quick reply.


I just tried that and it still errored out soon after I locked my device.


It called

application: UIApplication, handleEventsForBackgroundURLSession


before it errored out which I didn't notice before. The error was the same as before.


Edit: I believe it called `handleEventsForBackgroundURLSession` previously too.

I just tried that and it still errored out soon after I locked my device.

OK.

It called [

-application:handleEventsForBackgroundURLSession:completionHandler:
] before it errored out

Hmmm. Can you walk me through the sequence of events here? Here’s what I’ve got so far:

  1. Your app creates a background session with a fixed identifier.

  2. Your app starts a task in that background session.

  3. The user moves your app to the background by pressing the Home button.

  4. It’s not clear whether your app ever gets suspended but, regardless, shortly after moving to the background the system calls

    -application:handleEventsForBackgroundURLSession:completionHandler:
    .
  5. What happens then?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Has it any thing to do with `dataTask` and `downloadTask`?


I think `dataTask` doesn't work with `let config = URLSessionConfiguration.background(withIdentifier: "MySession")`

Has it any thing to do with

dataTask
and
downloadTask
?

That’s a good point, one that I hadn’t even considered. And the fact that the error info in the first post mentions

BackgroundDataTask
is solid evidence for your theory.

@fdsafasdfdas, are you using a downloading task (

URLSessionDownloadTask
) for your downloads? If you use a data task (
URLSessionDataTask
), you’ll see exactly this behaviour.

I think

dataTask
doesn't work with
let config URLSessionConfiguration.background(withIdentifier: "MySession")

That was true when

NSURLSession
was first introduced in iOS 7. In iOS 8 and later it’s possible to create a data task in a background session but that task will fail immediately if the app gets suspended. The ability to create a data task in a background session is useful in very obscure circumstances, but most folks never need to do this.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I am facing same issue, here is our discussion on reddit... I would use regular URLSession, but didBecomeActiveNotification does not work correctly and notify even if user in foreground.

https://www.reddit.com/r/iOSProgramming/comments/b5lj8r/help_with_configuring_background_request_with/

We had a similar problem with background uploads when the device is locked. Our workaround was to set NSFileProtectionNone on the <NSCachesDirectory>/com.apple.nsurlsessiond directory. One difference was that we were receiving NSPosixErrorDomain / code = 1 instead.


I thought we had filed a Radar on this because I'm not seeing it now.

Bill