Originator:rpatnayakuni
Number:rdar://37714680 Date Originated:
Status: Resolved:
Product:ios Product Version:11
Classification: Reproducible:
 
Same as https://bugreport.apple.com/web/?problemID=37714680

I'm trying to get the progress by KVO's on URLSessionDataTask's progress variable which is available in iOS 11. But I see that for downloads, I'm getting the exact progress but for uploads, I only get progress of 0, 0.95 and 1. 
I'm using below sample code (https://github.com/simplyi/ImageUploadExampleWithProgressBar), and changed it for my needs as in here.
https://stackoverflow.com/questions/48874967/ios-11-urlsessiondatatasks-progress-variable-is-not-showing-right-progress created.

Steps to Reproduce:
I'm using below sample code (https://github.com/simplyi/ImageUploadExampleWithProgressBar), and changed it for my needs 

Create a session and a sessionDataTask to upload a file as below. And start KVO 

let configuration = URLSessionConfiguration.default
    let session = URLSession(configuration: configuration)
    let task = session.dataTask(with: request) { (data, urlResp, error) in
        print("\(String(describing: data))")
    }
    if #available(iOS 11.0, *) {
        task.progress.addObserver(self, forKeyPath: #keyPath(Progress.fractionCompleted), options: [.new], context: &progressKVOContext)
        print("KVO set here")

    } else {
        // Fallback on earlier versions
    }
let configuration = URLSessionConfiguration.default
    let session = URLSession(configuration: configuration)
    let task = session.dataTask(with: request) { (data, urlResp, error) in
        print("\(String(describing: data))")
    }
    if #available(iOS 11.0, *) {
        task.progress.addObserver(self, forKeyPath: #keyPath(Progress.fractionCompleted), options: [.new], context: &progressKVOContext)
        print("KVO set here")

    } else {
        // Fallback on earlier versions
    }
    task.resume()


override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if context == &progressKVOContext && keyPath == #keyPath(Progress.fractionCompleted), let progress = object as? Progress {
        var progressFractionCompleted = (progress.totalUnitCount == -1) ? 1.0 : progress.fractionCompleted

        print("progressFractionCompleted = \(progressFractionCompleted)")
    } else {
        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
    }
}

Expected Results:
I should get exact upload progress printed.

Actual Results:
I only see a progress of 0, 0.95 and 1 % 

Version/Build:
iOS 11

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!