使用NSProgress查找下载速度和剩余时间

Eri*_*ric 10 macos ios ios7 osx-mavericks nsprogress

根据我看到的文档NSProgress,-[NSProgress localizedAdditionalDescription]可以报​​告下载速度和剩余时间,例如:

1.61 GB的3.22 GB(2 KB /秒) - 剩余2分钟

但是,当我NSProgress将a 关联到a 时,我无法获得这些细节NSURLSessionDownloadTask.这是我的代码:

Downloader.h

@interface Downloader : NSObject
@property NSProgress *overallProgress;
-(void)startDownload;
@end
Run Code Online (Sandbox Code Playgroud)

Downloader.m

- (void)startDownload {

    self.overallProgress = [NSProgress progressWithTotalUnitCount:100];

    [self.overallProgress setKind:NSProgressKindFile];
    [self.overallProgress setUserInfoObject:NSProgressFileOperationKindKey forKey:NSProgressFileOperationKindDownloading];


    [self.overallProgress becomeCurrentWithPendingUnitCount:100];
    [self work1];
    [self.overallProgress resignCurrent];
}

- (void)work1 {

    NSProgress *firstTaskProgress = [NSProgress progressWithTotalUnitCount:1];
    [firstTaskProgress setKind:NSProgressKindFile];
    [firstTaskProgress setUserInfoObject:NSProgressFileOperationKindKey forKey:NSProgressFileOperationKindDownloading];

    NSURL *downloadURL = [NSURL URLWithString:@"http://ipv4.download.thinkbroadband.com/200MB.zip"];
    NSURL *destinationDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
    NSURL *destinationURL = [destinationDirectory URLByAppendingPathComponent:[downloadURL lastPathComponent]];

    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLSessionDownloadTask *fileDownloadTask =
    [session downloadTaskWithURL:downloadURL
               completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error){

                   [[NSFileManager defaultManager] removeItemAtURL:destinationURL error:NULL];

                   [[NSFileManager defaultManager] moveItemAtURL:location toURL:destinationURL error:nil];

                   [firstTaskProgress setCompletedUnitCount:1];
               }];

    [fileDownloadTask resume];
}
Run Code Online (Sandbox Code Playgroud)

DownloadObserver.m

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    downloader = [Downloader new];

    [downloader addObserver:self
                 forKeyPath:@"overallProgress.fractionCompleted"
                    options:NSKeyValueObservingOptionNew
                    context:NULL];

    [downloader startDownload];

}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"%@", [downloader.overallProgress localizedAdditionalDescription]);
}
Run Code Online (Sandbox Code Playgroud)

这只打印出来:

0的100

零KB,100字节

如何localizedAdditionalDescription打印下载速度和剩余时间?

vok*_*lam 17

首先,totalUnitCount应该以字节为单位对应文件的大小.在下载过程中,我们会更改completedUnitCount反映的内容localizedAdditionalDescription.NSProgressKindFile是将这些值格式化为文件大小的提示.

要正确设置进度,我们需要使用NSURLSessionDownloadDelegate方法而不是块处理程序.在 - URLSession: downloadTask: didWriteData: totalBytesWritten: totalBytesExpectedToWrite:我们有所有要启动的信息(totalBytesExpectedToWrite)和更新(totalBytesWritten)进度.

static void *myContext = &myContext;

- (void)download {
    NSURL *downloadURL = [NSURL URLWithString:@"http://ipv4.download.thinkbroadband.com/10MB.zip"];
    NSURLSession *session =
    [NSURLSession
     sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
     delegate:self
     delegateQueue:[NSOperationQueue mainQueue]];

    [[session downloadTaskWithURL:downloadURL] resume];
}

#pragma mark - url session download delegate

- (void)URLSession:(NSURLSession *)session
      downloadTask:(NSURLSessionDownloadTask *)downloadTask
      didWriteData:(int64_t)bytesWritten
 totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
    if (!_progress) {
        _progress = [NSProgress progressWithTotalUnitCount:totalBytesExpectedToWrite];
        _progress.kind = NSProgressKindFile;
        [_progress addObserver:self forKeyPath:@"fractionCompleted" options:0 context:myContext];
    }

    _progress.completedUnitCount = totalBytesWritten;
    //[_overallProgress setUserInfoObject:@1024 forKey:NSProgressEstimatedTimeRemainingKey];
}

- (void)URLSession:(NSURLSession *)session 
      downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
{
    [_progress removeObserver:self forKeyPath:@"fractionCompleted"];
    _progress = nil;
    NSLog(@"finished: %@", location);
}

-(void)observeValueForKeyPath:(NSString *)keyPath
                     ofObject:(id)object
                       change:(NSDictionary *)change
                      context:(void *)context
{
    if (context == myContext) {
        self.label.text = [_progress localizedAdditionalDescription];
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
Run Code Online (Sandbox Code Playgroud)

实际上,NSProgress不做任何计算.它只是根据userInfo和其他类属性格式化本地化描述.请参阅文档中的常量部分.您可以使用这些值并比较本地化描述输出.

如果要显示剩余下载时间,可以使用NSProgressEstimatedTimeRemainingKey.坏消息是您需要手动计算剩余时间.参考:如何估计剩余的下载时间(准确)?

我试着设置NSProgressThroughputKey哪个表示数据处理的速度,以每秒字节数为单位.我预计这NSProgress将计算剩余时间,但它没有发生.

另请参阅AFNetworking源:AFURLSessionManagerTaskDelegate使用NSProgress上载和下载任务的实例.