Urlsessiondelegate Function Not Being Called

URLSessionDelegate methods not called

You must conform to the relevant protocols, e.g.:

extension MyNetworkManager: URLSessionDelegate {
// this is intentionally blank

// obviously, if you implement any delegate methods for this protocol, put them here
}

extension MyNetworkManager: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didWriteData bytesWritten: Int64,
totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64) {
print(#function)
}

func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL) {
print(#function)
}
}

extension MyNetworkManager: URLSessionTaskDelegate {
func urlSession(_ session: URLSession,
task: URLSessionTask,
didCompleteWithError error: Error?) {
print(#function, error ?? "No error")
}
}

If you don’t conform to URLSessionDownloadDelegate, it won’t call URLSessionDownloadDelegate methods.

URLSession delegate methods don't works

according the comment, i edited code and it's worked.

class ViewController: UIViewController, URLSessionTaskDelegate, URLSessionDelegate, URLSessionDataDelegate {

var httpString = "hided"

override func viewDidLoad() {
super.viewDidLoad()
getLogBinData()
}

func getLogBinData() {
let session = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue.main)
if let url = URL(string: httpString + "log.bin") {
var request = URLRequest(url: url)
request.httpMethod = "GET"
let task = session.dataTask(with: request)
task.resume()
}
}

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
if dataTask.countOfBytesReceived >= 500 {
print(dataTask.countOfBytesReceived)
}
}
}

`URLSessionDelegate` methods are not called

The func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) method you're implementing is part of URLSessionDownloadDelegate, not URLSessionDelegate, so you should declare conformance to URLSessionDownloadDelegate as well in order for the delegate call to occur.

URLSessionDataDelegate methods not being called

You only declare conformance to URLSessionDelegate in your extension. If you declare conformance to URLSessionDataDelegate instead, your delegate methods will be called.

URLSessionDelegate class deinit not called

URLSession keeps a strong reference to its delegate (self in your case). Check the official documentation:

The session object keeps a strong reference to the delegate until your app exits or explicitly invalidates the session. If you don’t invalidate the session, your app leaks memory until the app terminates.

You can invalidate the session using its methods finishTasksAndInvalidate() or invalidateAndCancel(). After that URLSession will release the strong reference to its delegate.

I guess you code example is just to demonstrate the behavior, but anyway I have to mention it's not a good practice to create a new URLSession for every single URLRequest.

URLSessionDataDelegate not being called using http2 connection

It's because the first 512 bytes are buffered: https://forums.developer.apple.com/thread/64875



Related Topics



Leave a reply



Submit