NSURLSessionDownloadTask并重试

时间:2015-07-07 20:55:51

标签: ios objective-c objective-c-blocks afnetworking-2

我使用以下代码从网上下载文件。我如何构造代码(使用块),如果它失败,它将以最大的retrycount重试。这是我用来下载文件的代码。

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURL *myURL = [NSURL URLWithString:@"urlstring"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL];

    [request setValue:token forHTTPHeaderField:@"Authorization"];

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSURL *directoryURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
        return [directoryURL URLByAppendingPathComponent:fileName];
        } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
              //check here if error then retry using block ?
                if (!error)
                 {
                     //do something with the file
                 }
                 else
                  {
                       //retry download ??
                  }
      }
  }];
   [downloadTask resume];

1 个答案:

答案 0 :(得分:0)

您可以创建一个retryCount实例变量,并将其设置为您想要重试网络呼叫的次数。然后,您可以将所有网络代码放在具有自定义完成处理程序的方法中。您的完成处理程序可以是一个以BOOL作为参数并返回void的块。然后,在您的网络代码中,根据网络调用是成功还是失败,创建本地BOOL变量并将其设置为YESNO。您将在网络代码中运行完成处理程序参数。然后,作为参数发送给方法的块(完成处理程序)可以“查看”方法中的局部变量是否设置为YESNO。如果是YES,则网络呼叫成功,所以什么都不做。如果是NO,则网络呼叫失败,因此请检查retryCount - 如果是> 0,再次调用网络方法并递减retryCount

<强>更新

这应该给你一个粗略的想法......

typedef void (^CustomCompletionHandler)(BOOL); // Create your CustomCompletionHandler type

- (void)getDataAndCallCompletionHandler:(CustomCompletionHandler *)completionHandler {
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURL *myURL = [NSURL URLWithString:@"urlstring"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL];

    [request setValue:token forHTTPHeaderField:@"Authorization"];

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

        BOOL success = NO; // Create local variable to keep track of whether network call succeeded or failed

        NSURL *directoryURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
        return [directoryURL URLByAppendingPathComponent:fileName];
        } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
              //check here if error then retry using block ?
                if (!error)
                 {
                     success = YES;
                     //do something with the file
                 }
                 else
                  {
                     success = NO;
                     //retry download ??
                  }
      }
  // Get main queue and call completionHandler and pass success:
  // completionHandler(success)
  }];
[downloadTask resume];
}

您可以通过执行类似......

之类的操作来调用此方法
[self getDataAndCallCompletionHandler:^(BOOL success) {
    // If success == true {  
    //     Do whatever you need to
    // } else {
    //     Check retryCount
    //     If you need to, call getDataAndCallCompletionHandler: again
    // }
}];