gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:41:00 25 4
gpt4 key购买 nike

我正在努力使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com