2013-01-18 41 views
2

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

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

[[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"); 
         }]; 
        } 
+0

您要求多少操作? – dbainbridge

+0

@dbainbridge我将所有要同步的数据发送到服务器,所以它取决于但可能很多 - 这就是为什么有些操作会超时。 – tugce

回答

3

我有同样的问题。我一直试图从FTP站点下载200多个文件,传递给enqueueBatchOfHTTPRequestOperations的NSURLRequests在第100个文件下载后会持续超时。我最终放弃了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。

+0

为什么你不使用异步请求?它是故意的,它可能会加快你的下载速度。 – tugce

+0

它已经在一个单独的线程/队列(downloadQueue)上,所以没关系。 – dbainbridge

+0

我的快速解决方案是增加超时秒数,但会利用你的。感谢你的分享! – tugce