2021 System Services
WWDC21 · 23 min · System Services
Reduce network delays for your app
CPU performance and network throughput rates keep improving, but the speed of light is one limit that isn’t going any higher. Learn the APIs and best practices to maximize your app’s responsiveness and efficiency by keeping network round-trip times low and minimizing the number of round trips when performing network operations.
Watch at developer.apple.com ↗Code shown on screen · 5 snippets
Fast open with TCP handshake
/* Allow fast open on the connection parameters */
parameters.allowFastOpen = true
let connection = NWConnection(to: endpoint, using: parameters)
/* Call send with idempotent initial data before starting the connection */
connection.send(content: initialData, completion: .idempotent)
connection.start(queue: myQueue) Sockets with fast open
connectx(fd, ..., CONNECT_DATA_IDEMPOTENT | CONNECT_RESUME_ON_READ_WRITE, ...); // delay SYN
write(fd, ...); // SYN goes out with first data segment Save round-trips when switching networks with Multipath TCP
// Multipath TCP
// Save multiple round-trips when switching networks
// On URLSessionConfiguration
let configuration = URLSessionConfiguration.default
configuration.multipathServiceType = .interactive
// On NWParameters
let parameters = NWParameters.tcp
parameters.multipathServiceType = .interactive Background service type, App in foreground
//Use default URLSession, set background on URLRequest
var request = URLRequest(url: myurl)
request.networkServiceType = .background
//Set service class on parameters to apply to the NWConnection
let parameters = NWParameters.tls
parameters.serviceClass = .background Time insensitive tasks running in background
//Configure background URL Session
lazy var urlSession: URLSession = {
let configuration = URLSessionConfiguration.background(withIdentifier: "MySession")
configuration.isDiscretionary = true
return URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
}() Related sessions
-
19 min -
20 min -
34 min -
1h 1m -
1h 2m -
53 min