3

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
Community
  • 1
  • 1
newDeveloper
  • 1,365
  • 1
  • 17
  • 27

2 Answers2

1

Check the iOS documentation correctly, alamoSessionManager.request will not work in the background, only alamoSessionManager.download and alamoSessionManager.upload only will work.

When you use alamoSessionManager.download JSON response will be download and saved in a tmp file. Make sure to move to a permanent place and read the file and convert it to JSONDecoder object. Let me know , if you are not clear about it.

Marwen Doukh
  • 1,946
  • 17
  • 26
Zumry Mohamed
  • 9,318
  • 5
  • 46
  • 51
0

Alamofire isn't really compatible with background sessions at the moment, both due to the inability to persist and reconnect its closure APIs, as well as the fact that it invalidateAndCancel() the URLSession when it's deinitd. We recommend you use URLSession directly for background session, or use foreground sessions with the background task API.

Jon Shier
  • 12,200
  • 3
  • 35
  • 37