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!