【问题标题】:Downloading a large number of images (1500+) efficiently with AFNetworking 2使用 AFNetworking 2 高效下载大量图像 (1500+)
【发布时间】:2015-04-04 16:52:27
【问题描述】:

我正在构建一个下载大量图像的应用程序,有时根据用户的要求下载 1500-5000 张图像。为此,我使用AFNetworking 2。起初,我只是遍历所有 URL,然后为每个 URL 发出请求。

for (NSString *url in urls) {
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        completion(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
    [requestOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        double percentDone = (double)totalBytesRead / (double)totalBytesExpectedToRead;
        progress(percentDone);
    }];
    [requestOperation start];
}

但是,在我获得大约 900 次下载/请求后,我会开始收到以下错误:

The request timed out

我假设这个错误直接来自AFNetworking

在不超时的情况下发出大量这样的下载请求的最佳和最有效的方法是什么?我应该使用dispatch_group 将请求批处理为outlined here 吗?

或者,我应该使用递归方法,一次下载一个图像,并且只有在第一个请求完成后才开始下一个请求?

【问题讨论】:

    标签: ios objective-c grand-central-dispatch batch-processing afnetworking-2


    【解决方案1】:

    试试这个

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setTimeoutInterval:600];  /* 10 minutes */
    

    但最好的解决方案是下载图像存档然后解压缩。

    【讨论】:

    • 在这种情况下,无法存档图像。
    • 设置这么长的超时间隔安全吗?
    • @NicHubbard 是的,它是安全的。默认情况下,AFNetworking 有 60 秒的超时间隔。
    猜你喜欢
    • 1970-01-01
    • 2013-10-30
    • 2014-05-07
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多