ios - 如何通过 NSOperationQueue 从多个 url 下载多个文件

标签 ios objective-c afnetworking-2 nsoperationqueue afhttprequestoperation

我正在努力使用 AFNetworking 实现多个文件的下载机制。我想从多个带有进度消息的 url 一个接一个地下载 zip 文件。我试过像下面的代码,但出现错误 -

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSOperationQueue addOperation:]: operation is already enqueued on a queue'

这是我的代码部分:

- (void) downloadCarContents:(NSArray *)urlArray forContent:(NSArray *)contentPaths {

    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];

    for (int i=0; i< urlArray.count; i++) {

        NSString *destinationPath = [self.documentDirectory getDownloadContentPath:contentPaths[i]];

        NSLog(@"Dest : %@", destinationPath);

        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        AFHTTPRequestOperation *operation = [manager GET:urlArray[i]
                                              parameters:nil
                                                 success:^(AFHTTPRequestOperation *operation, id responseObject) {

                                                 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

                                                     NSLog(@"Error : %ld", (long)error.code);
                                                 }];

        operation.outputStream = [NSOutputStream outputStreamToFileAtPath:destinationPath append:NO];

        [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
            float percentage = (float) (totalBytesRead * 100) / totalBytesExpectedToRead;
            [self.delegate downloadProgression:percentage];
        }];

        [operationQueue addOperation:operation];
    }
}

请帮忙。

最佳答案

当您调用 GET 时,它已经添加到 AFHTTPRequestionOperationManageroperationQueue 中。所以不要再将它添加到队列中。

此外,您应该在循环之前实例化一次 AFHTTPRequestOperationManager,而不是在循环中重复。


此代码还有其他问题,但与其尝试解决所有这些问题,我建议您过渡到使用 NSURLSessionAFHTTPSessionManager。旧的 AFHTTPRequestOperationManager 是基于 NSURLConnection 的,但现在已弃用 NSURLConnection。而且,事实上,AFNetworking 3.0 已经完全淘汰了 AFHTTPRequestOperationManager

因此,AFHTTPSessionManager 再现可能如下所示:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

for (NSInteger i = 0; i < urlArray.count; i++) {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlArray[i]]];
    NSURLSessionTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress) {
        [self.delegate downloadProgression:downloadProgress.fractionCompleted * 100.0];
    } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        return [NSURL fileURLWithPath:[self.documentDirectory getDownloadContentPath:contentPaths[i]]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        NSLog(@"File downloaded to: %@", filePath);
        NSLog(@"Error: %@" error.localizedDescription);
    }];
    [task resume];
}

关于ios - 如何通过 NSOperationQueue 从多个 url 下载多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34249124/

相关文章:

ios - 为什么选择器 View 不显示属性字符串?

ios - RestKit 只保存响应数组中的最后一项

php - iOS:在 PHP 服务文件中使用 return 而不是 echo 时出现 JSON 解析错误

iphone - 唯一设备标识符

ios - AFNetworking 和映射对象的方法

ios - 如何使用AFNetworking 2.0将所有网络请求限制为Wi-Fi连接?

ios - 在 iOS 的屏幕上和屏幕外动画 View

ios - swift : AVAudioPCMBuffer vs AVAudioBuffer vs AVAudioCompressedBuffer

ios - UIWebview 启用 cookie

objective-c - 从逗号分隔文件加载类的最佳方法