URLSessionDelegate Function Not Being Called

Viewed 3972

I would like to process data as it comes in, so I've instiated a URL session like so:

let session = URLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: operationQueue);

I also set the class to be a URLSessionDataDelegate:

class ViewController: UITableViewController, URLSessionDataDelegate{

Lastly, I implement the didReceive data function like this:

func urlSession(_ session: URLSession,
                dataTask: URLSessionDataTask,
                didReceive data: Data){
    print(data)
}

However, the function is never being called.

I run my session like this:

let session1 = session.dataTask(with: url) { (data, response, error) in
    print(data!);
}

It prints the data from the callback, but not from the delegate. Any help is greatly appreciated.

EDIT: I also added the following:

func urlSession(_ session: URLSession,
                dataTask: URLSessionDataTask,
                didReceive response: URLResponse,
                completionHandler: @escaping (URLSession.ResponseDisposition) -> Void){
    completionHandler(URLSession.ResponseDisposition.allow);
}

However, the delegate method is still not being called.

1 Answers
Related