Optimizing Performance with URLSession: Exploring multipathServiceType, waitsForConnectivity, and timeoutIntervalForRequest

Deepak Carpenter
Appgrid
Published in
2 min readJun 2, 2023

URLSession is a powerful framework in iOS and macOS that allows developers to interact with web services and retrieve data from the internet. In addition to its core functionality, URLSession provides built-in APIs that can enhance network connectivity and control request behavior. In this article, we will delve into three key URLSession APIs — multipathServiceType, waitsForConnectivity, and timeoutIntervalForRequest — and explore how they can be used to optimize performance. We’ll examine code snippets demonstrating their usage in practical scenarios.

multipathServiceType:
The multipathServiceType property of URLSessionConfiguration allows you to leverage multipath TCP (MPTCP) technology on devices that support it. By setting the multipathServiceType property, you can indicate that URLSession should prefer networks that support MPTCP, thereby improving performance and reliability. The following code snippet demonstrates how to set the multipathServiceType property to .interactive:

let config = URLSessionConfiguration.default
config.multipathServiceType = .interactive
let session = URLSession(configuration: config)
// Use the session for your network requests

waitsForConnectivity:
The waitsForConnectivity property of URLSessionConfiguration determines whether URLSession should wait for an internet connection before executing requests. Setting waitsForConnectivity to false allows URLSession to attempt requests immediately, even if there is no network connectivity. The code snippet below illustrates how to set waitsForConnectivity to false:

let config = URLSessionConfiguration.default
config.waitsForConnectivity = false
let session = URLSession(configuration: config)
// Use the session for your network requests

timeoutIntervalForRequest:
The timeoutIntervalForRequest property of URLSessionConfiguration allows you to specify a timeout duration for network requests. By default, URLSession sets the timeout interval to 60 seconds. Adjusting the timeout interval can help optimize performance by preventing your application from waiting indefinitely for a response. The following code snippet demonstrates how to set a custom timeout interval:

let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 30
let session = URLSession(configuration: config)
// Use the session for your network requests

Why should you use it?
Utilizing URLSession’s built-in APIs, such as multipathServiceType, waitsForConnectivity, and timeoutIntervalForRequest, can significantly impact the performance of your network operations. By optimizing network connectivity, controlling request behavior, and setting appropriate timeouts, you can ensure efficient data transfers and a responsive user experience. Incorporate these APIs into your URLSession configurations and fine-tune their values based on your specific application requirements. Remember to profile, test, and gather user feedback to further optimize performance and deliver seamless network interactions.

Inspired by Avanderlee for this post.

Cheers!
Happy Coding!

--

--