7

I'm using NSURLSession for the networking. Testing in Charlesproxy with connections drop shows there are actually three requests are being sent instead of one.

It looks like it is sort of NSURLSession low level work - if it determines connection drop, it will actually send several more requests before deciding there is really no network and calling delegate/block with failure.

Just to prove my assumptions, I changed NSURLSessionConfiguration from defaultSessionConfiguration to backgroundSessionConfigurationWithIdentifier and it started to send even more requests (4) before calling delegate/block.

This is not some issue in my opinion, just wondering wether it can be configured somehow.

Nikita
  • 1,811
  • 1
  • 20
  • 41
  • Are you sure it's resending the same request and not, for example, for example, responding to authentication challenges and/or following redirects? AFAIK, only session tasks on background `NSURLSession` will automatically retry if connection is not established. – Rob Aug 07 '16 at 16:22
  • it is happening not when connection is not established, but when it is being dropped, I mean not response with 3xx/4xx/5xx but explicit connection drop. The configuration I'm creating is default and not background. – Nikita Aug 08 '16 at 09:00
  • how are you explicitly dropping the request? – Wain Aug 10 '16 at 08:42
  • With Charlesproxy app -> Tools -> blacklist -> add endpoint to drop – Nikita Aug 10 '16 at 11:29
  • Just check reachability of your server before send the request using Apple Reachability api. eg: if (isReachable) { //creat reaquest & hit webService }else { //show reachability error } – SaRaVaNaN DM Aug 16 '16 at 12:29
  • 1
    how is that related to the question??? – Nikita Aug 16 '16 at 13:33
  • @Nikita: Have you already found a solution? I want to disable retry's as well... – phylib Jun 21 '18 at 18:56

1 Answers1

1

I think what you're seeing are artifacts of the way NSURLSession works. It:

  • Simultaneously makes IPv4 and IPv6 connections to the host.
  • Uses the first connection that opens successfully, closing the other connection immediately.
  • Upon failure, tells Reachability to see if it can reach any other servers (e.g. apple.com) to determine which error code to send back.

AFAIK, none of that is configurable, AFAIK.

dgatwood
  • 10,129
  • 1
  • 28
  • 49
  • I see several requests only in one case - if connection is being dropped (explicitly). In all other cases there are always one request. Moreover all three requests are absolutely identical and go to ipv4 address. So I don't think this is the case. – Nikita Aug 09 '16 at 09:13
  • That's very odd. Are you sure you don't have a bunch of queued up requests waiting in the session, gated by the maximum number of concurrent requests? – dgatwood Aug 09 '16 at 20:03