2

我正在尝试使用异步操作请求,但在某些时候操作请求由于请求超时而失败。我如何形成我的块,以便在所有操作完成失败或完成但没有超时时重新发送超时操作并执行一些操作。

我真的需要弄清楚这一点,非常感谢!

[[SDAFParseAPIClient sharedClient] enqueueBatchOfHTTPRequestOperations:pagedOperations progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {
                    NSLog(@"PAGED totalNumberOfOperations: %u  numberOfCompletedOperations: %u",totalNumberOfOperations,numberOfCompletedOperations);
                } completionBlock:^(NSArray *operations) {

                    NSMutableArray *retryops = [NSMutableArray array];

                    for(AFHTTPRequestOperation *operation in operations){
                        if(operation.error.code == -1001){
                            NSLog(@"error code %d",operation.error.code);

                            [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                                NSLog(@"comp");
//actually original completion block is needed
                            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                NSLog(@"fail");
  //actually original fail block is needed
                            }];
                            [retryops addObject:operation];
                            //Do something with the response
                        }else{
                            //Handle the failure
                        }
                    }

                    if ([retryops count] == 0) {
                        NSLog(@"all paginated operations completed");
                    }
                    else {
                        [[SDAFParseAPIClient sharedClient] enqueueBatchOfHTTPRequestOperations:retryops progressBlock:
                         ^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {

                         } completionBlock:
                         ^(NSArray *operations) {
                             NSLog(@"all retry paginated operations completed");
                         }];
                    }
4

1 回答 1

3

我有同样的问题。我一直试图从 FTP 站点下载 200 多个文件,并且在下载第 100 个文件后,传递给 enqueueBatchOfHTTPRequestOperations 的 NSURLRequests 会一直超时。我最终完全放弃了 AFNetworking,因为它不需要并且使解决方案复杂化。这部分是其他几个 SO 答案的混合。

NSArray *requestsArray = // my array of [[NSMutableURLRequest alloc] initWithURL:url]'s
dispatch_queue_t downloadQueue = dispatch_queue_create("downloadQueue", NULL);
dispatch_async(downloadQueue, ^{
    NSURLResponse *response;
    NSError *requestError;
    for (NSURLRequest *request in requestsArray) {
        response = nil;
        requestError = nil;
        NSData *requestData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
        if (response == nil) {
            // Check for problems
            if (requestError != nil) {

            }
        }
        else {
            // Data was received.. continue processing
            // Make sure to do any GUI related updates in main queue/thread,
            // not directly from here
        }
    }
});

这使用了一个串行调度队列,因此每个同步 NSURLRequest 一次执行一个。当每个请求完成时,我会处理其中的数据,而不是等待所有请求完成。因为这不在主队列上,所以它不会阻塞 GUI。

于 2013-03-18T19:49:04.677 回答