0

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?

AppsDev
  • 12,319
  • 23
  • 93
  • 186
  • you must have seen this https://stackoverflow.com/questions/24057525/swift-native-base-class-or-nsobject/ – Amr Eladawy May 07 '17 at 17:00
  • @AmrElAdawy thanks, but that post looks like providing quite generic guidelines for me. What about my particular case, that is, a singleton that performs networking? – AppsDev May 07 '17 at 17:11
  • I think I am not fully getting your question, but I had a look on this post https://stackoverflow.com/questions/39943371/swift-3-subclassing-nsobject-or-not It has some good explanation about extending NSObject or not. – Amr Eladawy May 07 '17 at 21:02

0 Answers0