I've reading several examples related to URLSession
and URLSessionTask
where code snippets were shown and/or the sample project was available to download. Examples were about the quite common scenario of requesting some info to a REST service by using URLSessionDataTask
, most of them having classes similar to this:
var defaultSession: URLSession
var dataTask: URLSessionDataTask?
override init() {
let configuration = URLSessionConfiguration.default
self.defaultSession = URLSession(configuration: configuration, delegate: nil, delegateQueue: self.operationQueue)
super.init()
}
func callService(with searchUrl: URL, completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
dataTask = defaultSession.dataTask(with: searchUrl, completionHandler: completion)
dataTask?.resume()
}
I find that usually a reference to dataTask
is kept but it is not used at any other place out of that func. Which would be the reasons to keep a reference to the dataTask
?