AFNetworking - Download multiple files + monitorin

2019-03-08 12:11发布

I am trying to change my code from ASIHTTPRequest to AFNetworking. Currently I want to select 10-15 different HTTP URLs (files) and download them to a documents folder.

With ASIHTTPRequest that was pretty easy with

[myQueue setDownloadProgressDelegate:myUIProgressView];

In AFNetworking I can't figure out how to do it. I have the following code which downloads the files, stores them and notifies when a file downloads successfully, but I can't create the progress bar for this queue with total size.

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];  
}

3条回答
Evening l夕情丶
2楼-- · 2019-03-08 12:28

Imagine downloading 200+ files this way by assuming a file size of 1 MB each. What happens when you create such a bunch of requests (with a default timeout of 30s)? Right after 30 seconds you will be bombed by timeout errors.

Just sayin' Martin

查看更多
我想做一个坏孩纸
3楼-- · 2019-03-08 12:33

I think you will have to create your own UIProgressView, which I will call progressView for the example.

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

Then just update the progress bar:

[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);
}];
查看更多
孤傲高冷的网名
4楼-- · 2019-03-08 12:43
[operation setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {

    float percentDone = ((float)((int)totalBytesRead) / (float)((int)totalBytesExpectedToRead));

    progressView.progress = percentDone;

}];
查看更多
登录 后发表回答