Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the correct usage of URLSession, create new one or reuse same one

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.

like image 831
Leem Avatar asked Oct 01 '18 13:10

Leem


2 Answers

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.

like image 148
Aakash Avatar answered Oct 01 '22 23:10

Aakash


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.

like image 40
dgatwood Avatar answered Oct 02 '22 00:10

dgatwood