Reputation: 12842
I have an app that makes a whole bunch of different REST calls to the same server from a bunch of different view controllers. What’s best practice with regard to URLSession
: Share the same URLSession object? Or just the URLSessionConfiguration object? Or doesn’t matter about either?
For example, when making request to an endpoint, should I
Instantiate a brand new URLSession
each request with with the shared URLSessionConfiguration
?
Instantiate a single URLSession
once for the current active app instance, and reuse it across all requests?
Upvotes: 2
Views: 767
Reputation: 384
Instantiate a single URLSession connection once for the current active app instance, and reuse it across all requests. It fasts the speed of connecting and getting response from rest API. New URLSession for each request slow the app performance and memory leaks. Multiple URLSession can be created if need different URLSessionConfiguration for any other requests.
class MyTaskManager {
private lazy var sessionManager: URLSession = {
let configuration = URLSessionConfiguration.default
configuration.waitsForConnectivity = true
configuration.timeoutIntervalForRequest = 300 // seconds
configuration.timeoutIntervalForResource = 300 // seconds
return URLSession(configuration: configuration)
}()
func postMyData(...) {
dataTask = sessionManager.dataTask(with: url) { data, response, error in
...
}
dataTask.resume()
}
func getMyData(...) {
dataTask = sessionManager.dataTask(with: url) { data, response, error in
...
}
dataTask.resume()
}
}
Upvotes: 0
Reputation: 3585
It is not best practice to create multiple URLSessions. Apple recommends creating only one if possible:
WWDC2017 Advances in Networking, Part 2
"We have seen developers that take their old NSURLConnection code and convert it to the new URLSession code by mechanically making a URLSession for every old NSURLConnection they used to have. This is very inefficient and wasteful. For almost all of your apps what you want to have is just one URLSession, which can then have as many tasks as you want. The only time you would want more than one URLSession is when you have groups of different operations that have radically different requirements. And in that case you might create two different configuration objects and create two different URLSessions using those two configuration objects."
Though the Apple developer presenting this session was answering a slightly different question, clearly the answer he gives is good for your question too.
Upvotes: 5
Reputation: 70956
A long-lived shared URLSession
object only makes sense if you need to use methods on that class that affect multiple tasks at the same time. For example if you need to call getTasksWithCompletionHandler(_:)
or finishTasksAndInvalidate()
, the session object needs to exist for long enough to cover all of the tasks you want those methods to affect.
It might also make sense if creating them on the fly would result in having several identical instances at the same time.
Otherwise, create a URLSession
when you need it and then let it get deallocated when you don't.
In either case I wouldn't keep a shared URLSessionConfiguration
object in memory at all times. Set up a factory method that can create one, and call that whenever you need a URLSession
.
Upvotes: 0