I'm checking an example of the use of URLSession
and the networking tasks are performed by a singleton class that is more or less like this:
class NetworkManager: NSObject {
static let sharedInstance = NetworkManager()
fileprivate var session: URLSession
fileprivate var dataTask: URLSessionDataTask?
override init() {
let configuration = URLSessionConfiguration.default
session = URLSession(configuration: configuration)
super.init()
}
func search(searchUrl: URL, completion: @escaping NetResult) {
dataTask?.cancel()
dataTask = session.dataTask(with: searchUrl, completionHandler: completion)
dataTask?.resume()
}
// Some other methods
}
This class also conforms to URLSessionDownloadDelegate
in an extension
.
What is the benefit of subclassing NSObject
here?