em>该文件已被下载。I've also tried to call setDownloadTaskDidWriteDataBlock on the manager, and I do get progress notifications there but I receive them all grouped together after the file has been downloaded.似乎这个区域在AFNetworking 2.0中仍然有点儿错误Seems like this area is still a bit buggy in AFNetworking 2.0有什么想法吗?推荐答案你应该观察 fractionCompleted 属性code> NSProgress 使用KVO的对象:You should observe the fractionCompleted property of your NSProgress object using KVO:NSURL *url = [NSURL URLWithString:@"http://www.hfrmovies.com/TheHobbitDesolationOfSmaug48fps.mp4"];NSURLRequest *request = [NSURLRequest requestWithURL:url];AFHTTPSessionManager *session = [AFHTTPSessionManager manager];NSProgress *progress;NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { // …} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { [progress removeObserver:self forKeyPath:@"fractionCompleted" context:NULL]; // …}];[downloadTask resume];[progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:NULL];然后添加观察者方法:- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ if ([keyPath isEqualToString:@"fractionCompleted"]) { NSProgress *progress = (NSProgress *)object; NSLog(@"Progress… %f", progress.fractionCompleted); } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; }}当然,你应该检查 keyPath 和/或 object 参数,以确定这是否是您要观察的对象/属性。Of course, you should check keyPath and/or object parameters to decide if that's the object/property you want to observe.您还可以使用 AFURLSessionManager 中的 setDownloadTaskDidWriteDataBlock:方法(从中 AFHTTPSessionManager inherits)设置一个接收下载进度更新的块。You can also use the setDownloadTaskDidWriteDataBlock: method from AFURLSessionManager (from which AFHTTPSessionManager inherits) to set a block for receiving download progress updates.[session setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) { NSLog(@"Progress… %lld", totalBytesWritten);}];此AFNetworking方法映射 URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite: 方法从 NSURLSessionDownloadDelegate 协议到更方便的块机制。This AFNetworking method maps the URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite: method from NSURLSessionDownloadDelegate protocol to a more convenient block mechanism. BTW,Apple的KVO实施严重受损。我建议使用更好的实现,例如Mike Ash在 MAKVONotificationCenter 中提出的实现。如果您有兴趣阅读为什么Apple的KVO坏了,请阅读 Key-Value Observing Right Right Mike Ash。BTW, Apple's KVO implementation is severely broken. I recommend using a better implementation like the one proposed by Mike Ash with MAKVONotificationCenter. If you are interested in reading why Apple's KVO is broken, read Key-Value Observing Done Right by Mike Ash. 这篇关于如何在AFNetworking 2.0中获得下载进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-15 05:13