2013-08-12 77 views
2

我使用AFDownloadRequestOperation + AFNetworking下载和从服务器恢复的文件列表。该代码一次很好地下载和恢复多个文件。但是,如何将操作队列中的所有操作排队并逐个执行操作?下载多个文件AFDownloadRequestOperation

这里是我当前的代码

// request the video file from server 
NSString *downloadURL = [NSString stringWithFormat:@"%@%@", [recipe download_url], [step valueForKey:@"video"]]; 

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:downloadURL]]; 
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:videoFile shouldResume:YES]; 

// done saving! 
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"Done downloading %@", videoFile); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %ld", (long)[error code]); 
}]; 

// set the progress 
[operation setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) { 
    float progress = ((float)totalBytesReadForFile)/totalBytesExpectedToReadForFile; 
    [progressBar setProgress:progress]; 
}]; 

[operation start]; 

回答

0

你要么需要将操作添加到AFHTTPClient的operationQueue,或将它们添加到您所做自己一个NSOperationQueue。

然后设置最大并发操作数使用1个

0

改变队列您

[operation start]; 

[[YourAFHTTPClientSubclass sharedInstance] enqueueHTTPRequestOperation:operation]; 

默认情况下,这将有操作的最大数目“由NSOperationQueue对象根据当前系统条件动态确定”。

如果你真的要强制所有内容一次一个,这样做:

[YourAFHTTPClientSubclass sharedInstance].operationQueue.maxConcurrentOperationCount = 1; 

这将阻止所有的网络操作,直到操作完成。

当然,你可以使自己的操作队列中的Audun建议,但它可能会更好,让系统决定基于当前条件什么。

根据你的使用情况,您可能希望将视频下载的操作的优先级设置为低:

operation.queuePriority = NSOperationQueuePriorityLow 

这将允许其他网络运营在更高的优先级被置于队列比你的视频下载。