1

I am trying to report the download of a son file from a server to the requester through an instance of NSProgress. However, when I set the totalUnitCount and completedUnitCount values, these are simply not updating on the NSProgress object.

This is how I initially create the progress object:

let progressUserInfo: [ String: String ] = [
    NSProgressFileOperationKindKey: NSProgressFileOperationKindDownloading,
]
let progress = NSProgress(parent: nil, userInfo: progressUserInfo)
progress.kind = NSProgressKindFile
progress.totalUnitCount = -1
progress.completedUnitCount = 0

I then keep an instance of this progress object around in the download manager and also pass it around to the caller that triggered the download.

Now, when I receive a server response I update it with the following method:

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
    progress.totalUnitCount = response.expectedContentLength
    completionHandler(.Allow)
}

Then, each time I get a chunk of data from the server, I update the progress:

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
    progress.totalUnitCount = dataTask.countOfBytesExpectedToReceive
    progress.completedUnitCount = dataTask.countOfBytesReceived
}

To check if this works, I added a KVO for the fractionCompleted property. This observer fires each time I update the completedUnitCount property, however, when checkin the values on the progress object, nothing has changed there:

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
    let progress = object as NSProgress
    println("loaded user: \(progress.localizedDescription) - \(progress.localizedAdditionalDescription) = \(progress.fractionCompleted)")
}

This always outputs loaded user: Downloading files… - = 0.0

I currently have no clue what is going on here and if this is some kind of swift issue or a misuse of the NSProgress api.

Thanks for your help in advance!

//EDIT: Despite the fact that debugging is totally broken for me in Swift and I can not po progress in the debugger, when doing a debugPrint() on the various places, the output I get is the following:

<NSProgress: 0x7f9473520f20> : Parent: 0x0 / Fraction completed: 0.0000 / Completed: 0 of -1
<NSProgress: 0x7f9473520f20> : Parent: 0x0 / Fraction completed: 0.0000 / Completed: 0 of -1
<NSProgress: 0x7f9473520f20> : Parent: 0x0 / Fraction completed: 0.0000 / Completed: 0 of -1

So I am definitely using the same NSProgress instance everywhere!

Michael Ochs
  • 2,846
  • 3
  • 27
  • 33
  • Don't you have to *add* the countOfBytesReceived to completedUnitCount? – Martin R Mar 05 '15 at 20:56
  • Have you checked that both progress objects are referring to the same instance? I just ran your code and it updated without any issues. – kellanburket Mar 05 '15 at 22:46
  • @MartinR yes, probably. But that would mean the progress should still be greater than 0 after the first package. – Michael Ochs Mar 06 '15 at 08:50
  • @blacksquare At the moment, that is the only NSProgress instance I am creating in the whole app, but I'll check. – Michael Ochs Mar 06 '15 at 08:50
  • @blacksquare it is the same progress! See my update above! – Michael Ochs Mar 06 '15 at 20:52
  • I am starting to think that there is an issue with the server involved in this. No i suddenly start getting values for the completed unit count, only the expected one is staying at -1, which might be a missing header. I don't know why I am suddenly getting completed values without touching my source code at all, though... – Michael Ochs Mar 06 '15 at 21:04
  • Do you need to handle the case where `countOfBytesExpectedToReceive == NSURLSessionTransferSizeUnknown /* -1 */`, i.e. if the server does not provide a `Content-Length` header? – penfold Oct 13 '15 at 00:14

0 Answers0