How to get upload progress when using "await urlSession.upload()"

How to get upload progress when using:

let (data, urlResponse) = try await urlSession.upload(
    for: urlRequest,
    from: bodyData,
    delegate: nil   // Something I need here maybe?
)

I have tried using:

func urlSession(
        _ session: URLSession,
        task: URLSessionTask,
        didSendBodyData bytesSent: Int64,
        totalBytesSent: Int64,
        totalBytesExpectedToSend: Int64) {
        
        print("fractionCompleted  : \(Float(totalBytesSent) / Float(totalBytesExpectedToSend))")
            
}

Within the same class but it never fires.

I specifically want the async / await capability on the upload.

I couldn't get the session.uploadTask to work with the await prefix.

Replies

Something I need here maybe?

Correct. Use self, if you implement urlSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:) in the same class.

(Please do not forget to make the class conform to URLSessionTaskDelegate.)

What if I have multiple parallel upload operations and need to map the URLSession in the delegate method to a progress indicator? Would I need a separate actor to manage this mapping?