排队所有失败的传输

时间:2013-12-10 12:19:10

标签: ios afnetworking nsoperationqueue

我正在编写一个需要将一些XML发送到服务器的客户端/服务器应用程序。

NSMutableArray *operations = [NSMutableArray array];
AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];

[operation1 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    float progress = (float)totalBytesRead / totalBytesExpectedToRead;
    NSLog(@"Progress 1 = %f",progress);
}];
[operations addObject:operation1];

AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];

[operation2 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    float progress = (float)totalBytesRead / totalBytesExpectedToRead;
    NSLog(@"Progress 2 = %f",progress*100);
}];
[operations addObject:operation2];






NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
// Set the max number of concurrent operations (threads)
[operationQueue setMaxConcurrentOperationCount:3];
[operationQueue addOperations:@[operation1, operation2] waitUntilFinished:NO];

我现在要做的是处理传输是否失败并有队列,以便重新发送。

使用AFNetworking库实现这一目标的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

首先,重新尝试失败的操作不是很明智。根据错误来源的不同,您可能会面临严重的副作用,例如重复提交。

您已经在使用AFHTTPRequestOperation,因此最简单的解决方案就是调用 setCompletionBlockWithSuccess:failure:并处理“失败”块中的错误。毕竟,您可能还希望在下载成功完成后使用“success”-block。

关于您提供的代码的最后一个细节:您正在第1行创建NSArray *操作 - 但由于您在最后一行创建了一个新的操作数组,因此您没有使用它。所以要么你留下了一些东西,要么你应该简化它。