2

I have an app that lets the user send messages with images. A user might hit send, then immediately close their phone or switch to another app.

We were running into an issue that if there's temporarily a bad network connection the message would fail to send. We switched to using NSURLSession backgroundConfigurationWithIdentifier so that backgrounding the app doesn't immediately time out the running request. We switched to using this for all our api requests, thinking that it wouldn't hurt for every request to able to continue in the background if the app were closed at the wrong time.

Fast forward a couple weeks we're noticing all requests seem slow. Using wireshark I just discovered that this background session seems to use a new http connection per request, meaning it requires setting up a TCP connection and new TLS handshake for every request, which was adding a ~500ms latency on every request in our app. This is a pretty big deal but I can't find this behavior documented anywhere, including the link above or Apple's background transfer considerations.

So my question is, is this behavior expected, or am I doing something wrong somewhere? Is there an easy way with NSURLSession to make an HTTP request that will use an existing keep-alive connection if there is one, but can fall back to the backgroundConfiguration if the app gets moved to the background?

danny
  • 10,103
  • 10
  • 50
  • 57

1 Answers1

0

NSURLSession is the recommended way to fulfill your use case. Have you tried setting backgroundSessionConfig.discretionary = true

iOS Reference

A Boolean value that determines whether background tasks can be scheduled at the discretion of the system for optimal performance.

If that doesn't help, I recommend filing a bug with iOS.

Lightbeard
  • 4,011
  • 10
  • 49
  • 59
  • I have tried `discretionary = true` and it also doesn't work. I'm not sure but I read that option to mean it might delay the run of the request if it's in the background and iOS is busy, I wouldn't expect it to have any effect if the app is in the foreground. I have filed a bug with Apple, we'll see what they say. – danny Aug 31 '16 at 05:57