【问题标题】:AFNetworking - Download multiple files + monitoring via UIProgressViewAFNetworking - 通过 UIProgressView 下载多个文件 + 监控
【发布时间】:2012-01-29 18:56:15
【问题描述】:

我正在尝试将我的代码从 ASIHTTPRequest 更改为 AFNetworking。目前我想选择 10-15 个不同的 HTTP URL(文件)并将它们下载到一个文档文件夹中。

使用 ASIHTTPRequest 非常简单

[myQueue setDownloadProgressDelegate:myUIProgressView];

在 AFNetworking 中,我不知道该怎么做。我有以下代码下载文件、存储文件并在文件下载成功时发出通知,但我无法为这个队列创建总大小的进度条。

for (i=0; i<3; i++) {

    NSString *urlpath = [NSString stringWithFormat:@"http://www.domain.com/file.zip"];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlpath]];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"testFile%i.zip",i]];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully downloaded file to %@", path);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    [operation setDownloadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
        NSLog(@"Sent %d of %d bytes, %@", totalBytesWritten, totalBytesExpectedToWrite, path);
    }];

    [myQueue addOperation:operation];  
}

【问题讨论】:

  • 您好 John,如果您能接受我的回答,如果它对您有用,或者如果它不适合您,请告诉我们问题是什么?
  • 我认为是因为您提示了如何为一次下载设置进度HUD,而约翰想要的是整个队列的进度条。

标签: iphone ios asihttprequest afnetworking


【解决方案1】:

我认为您必须创建自己的 UIProgressView,我将其称为 progressView 作为示例。

progressVu = [[UIProgressView alloc] initWithFrame:CGRectMake(x, y, width, height)];
[progressVu setProgressViewStyle: UIProgressViewStyleDefault];

那就更新进度条吧:

[operation setDownloadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {

    float percentDone = ((float)((int)totalBytesWritten) / (float)((int)totalBytesExpectedToWrite));

    progressView.progress = percentDone;

    NSLog(@"Sent %d of %d bytes, %@", totalBytesWritten, totalBytesExpectedToWrite, path);
}];

【讨论】:

    【解决方案2】:
    [operation setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    
        float percentDone = ((float)((int)totalBytesRead) / (float)((int)totalBytesExpectedToRead));
    
        progressView.progress = percentDone;
    
    }];
    

    【讨论】:

    • 此代码显示错误Incompatible block pointer types sending 'void (^)(NSInteger, long long, long long)' to parameter of type 'void (^)(NSUInteger, long long, long long)'
    【解决方案3】:

    假设每个文件大小为 1 MB,以这种方式下载 200 多个文件。 当你创建这样一堆请求(默认超时为 30 秒)时会发生什么? 30 秒后你会被超时错误轰炸。

    只是说 马丁

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多