isReusedConnection can be true or false for both HTTP/1.1 of HTTP/2 protocols because it's depends on URLSession's alive connections.
HTTP/1.1
URLSession creates a connection pool of 4 connections to a single domain by default and use them to send requests e.g.:
var requestNum = 0
class Metrics : NSObject, URLSessionDataDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
for metric in metrics.transactionMetrics {
print("\(requestNum). protocol: \(metric.networkProtocolName!), reused: \(metric.isReusedConnection)")
requestNum += 1
}
}
}
let metrics = Metrics()
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: metrics, delegateQueue: nil)
func makeRequests(_ http: String) {
requestNum = 0
for i in 0...10 {
let request = URLRequest(url: URL(string: "https://\(http).akamai.com/demo/tile-\(i).png")!)
let task = session.dataTask(with: request)
task.resume()
}
}
makeRequests("http1")
Outputs:
0. protocol: http/1.1, reused: false
1. protocol: http/1.1, reused: true
2. protocol: http/1.1, reused: true
3. protocol: http/1.1, reused: false
4. protocol: http/1.1, reused: false
5. protocol: http/1.1, reused: false
6. protocol: http/1.1, reused: true
7. protocol: http/1.1, reused: true
8. protocol: http/1.1, reused: true
9. protocol: http/1.1, reused: true
10. protocol: http/1.1, reused: true
As you can see there are 4 new connections (reused: false) are created for the session and all next requests reuse them later.
HTTP/2
URLSession works the same as for HTTP/1.1 but creates a single connection to a domain and sends all requests through it e.g.:
makeRequests("http2")
Output:
0. protocol: h2, reused: false
1. protocol: h2, reused: true
2. protocol: h2, reused: true
3. protocol: h2, reused: true
4. protocol: h2, reused: true
5. protocol: h2, reused: true
6. protocol: h2, reused: true
7. protocol: h2, reused: true
8. protocol: h2, reused: true
9. protocol: h2, reused: true
10. protocol: h2, reused: true
New connection was created for the first request only from above and next requests just reuse it later.
Connection lifetime
URLSession is designed for typically client applications and doesn't hold connections to a server all the time. Connections are alive about 20 secs and then the session closes them so it creates new connections each time for not frequent requests.