I am using URLSession in my iOS project. (Swift 4). The following code is only for illustration purpose.
class MyTaskManager {
...
func postMyData(...) {
let defaultSession = URLSession(configuration: .default)
dataTask = defaultSession.dataTask(with: url) { data, response, error in
...
}
dataTask.resume()
}
func getMyData(...) {
let defaultSession = URLSession(configuration: .default)
dataTask = defaultSession.dataTask(with: url) { data, response, error in
...
}
dataTask.resume()
}
}
I am trying to understand the best practice of using URLSession
in the sense of whether each function call of making HTTP request should create a new URLSession
or should I create a global one & all the calls to HTTP requests should use the same URLSession
instance?
I have studied on internet, there is an accepted answer which says I should create a new URLSession for each function/request call , there is/are also suggestions that I should reuse the same URLSession. I get confused by those accepted but conflicting answers. Could someone clarify for me the correct answer to this question?
My application doesn't have upload or download tasks, only pure RESTful request with JSON data format. No multiple configurations needed either.
You should create a shared instance of the data session and use the same creating multiple tasks because it's rarely the case that you need to have a different configuration for an api.
I suggest and use the shared instance of data session for getting data from an api.
class MyTaskManager {
static let sessionManager: URLSession = {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 30 // seconds
configuration.timeoutIntervalForResource = 30 // 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()
}
}
The benefit of this is that I had to create the session only once, so that will save repetition of same code and also the process to initialise the same thing again per api request. This will be more helpful in case you need to more custom configuration of the session.
Most of the time, you should use a single session for all of your work. This allows the session to limit simultaneous requests to a single host (limiting the potential for accidental abuse), and also is significantly more memory efficient than using a new session for each request.
If you have multiple groups of tasks that need to be canceled as a group (for example, uploading all of the images in a new album, downloading all the resources for loading a single web page, etc.), or multiple requests that need a different configuration (e.g. background downloads) then it makes sense to use one session per group or configuration.
Alternatively, if you don't need to make any configuration changes to the default and are happy with just running a block when each request finishes, you can also use the shared session ([NSURLSession sharedSession]
) rather than creating a session at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With