0

在初始化 AFHTTPClient 的共享实例后,我有一个 setReachabilityStatusChangeBlock 代码块,然后我有一个 enqueueBatchOfHTTPRequestOperations。问题是 setReachabilityStatusChangeBlock 永远不会被执行,我试图捕捉可能危及 enqueueBatchOfHTTPRequestOperations 中下载的任何文件的不良网络连接。

对此的任何帮助将不胜感激。

这是我所拥有的一个例子......

////////////////////////
// Start the operations in the download client
////////////////////////
AFHTTPClient *client = [EMEDownloadClient sharedClient];

// Workaround if network connection is poor
[client setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

  NSLog(@"%d", status);

  if ( status == AFNetworkReachabilityStatusNotReachable ) {
    NSLog(@"Reachability Changed : disconnected");

    // update status for download
    [dq statusDownload:@"removed"];
    // remove assetId from the downloadQueue
    [dq resetDownloadQueue];

    dispatch_async(dispatch_get_main_queue(), ^{
      [SVProgressHUD showErrorWithStatus:
       @"There is no network connection, please try again!"];
    });

  }
  else if ( status == AFNetworkReachabilityStatusUnknown ) {
    NSLog(@"Reachability Changed : unknown");

    // update status for download
    [dq statusDownload:@"removed"];
    // remove assetId from the downloadQueue
    [dq resetDownloadQueue];

    dispatch_async(dispatch_get_main_queue(), ^{
      [SVProgressHUD showErrorWithStatus:
       @"Poor internet connection, please try again from a better \n"
       "location."];
    });

  }
  else {
    NSLog(@"Reachability Changed : connected");
  }

}];


[client enqueueBatchOfHTTPRequestOperations:requestsForDownload
      progressBlock:^(NSUInteger numberOfFinishedOperations,
                          NSUInteger totalNumberOfOperations) {

        NSLog(@"%d / %d", numberOfFinishedOperations, totalNumberOfOperations);

        dispatch_async(dispatch_get_main_queue(), ^{

          [SVProgressHUD showWithStatus:[NSString
              stringWithFormat:@"Downloading... %d / %d.   This process \n"
              "may take a few minutes for assets with multiple playback \n"
              "components.",
              numberOfFinishedOperations,
              totalNumberOfOperations]

              maskType:SVProgressHUDMaskTypeGradient];

        });

      } completionBlock:^(NSArray *operations) {
        int i = 0;
        for (AFHTTPRequestOperation *ro in operations) {
          NSLog(@"Operation statusCode: %ld", (long)[ro.response statusCode]);
          if ((long)[ro.response statusCode] != 200 ) {
              i++;
          }
        }

        if ( i == 0 ) {
          ////////////////////////
          // Save the managedObjectContext
          ////////////////////////
          NSError *error = nil;

          if ([context save:&error]) {

            // Sucess!!

            // NSLog(@"%s", __PRETTY_FUNCTION__);
            NSLog(@"context used in downloading has been saved");

            // update status for download
            [dq statusDownload:@"downloaded"];
            // remove assetId from the downloadQueue
            [dq resetDownloadQueue];

            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showSuccessWithStatus:@"Download Completed"];
            });

            if (autoplay) {

              if ([section isEqualToString:@"generalLibrary"]) {

                // autoplay downloaded asset
                [[NSNotificationCenter defaultCenter]
postNotificationName:kECHONotificationDownloadAssetDidSucceedAutoplayGeneral
                                   object:self
                                 userInfo: @{ @"assetID": assetID }];

              } else if ([section isEqualToString:@"collectionLibrary"]) {

                // autoplay downloaded asset
                [[NSNotificationCenter defaultCenter] postNotificationName:kECHO
                                   object:self
                                 userInfo: @{ @"assetID": assetID }];

              } else if ([section isEqualToString:@"detailView"]) {

                // autoplay downloaded asset
                [[NSNotificationCenter defaultCenter]
postNotificationName:kECHONotificationDownloadAssetDidSucceedAutoplayDetail
                                   object:self
                                 userInfo: @{ @"assetID": assetID }];
              }

            }

          } else {

            NSLog(@"ERROR: %@ %@", [error localizedDescription],
                      [error userInfo]);

            exit(1);
          }
        } else {

          // something went wrong with the download, try again

          // update status for download
          [dq statusDownload:@"removed"];
          // remove assetId from the downloadQueue
          [dq resetDownloadQueue];

          dispatch_async(dispatch_get_main_queue(), ^{
              [SVProgressHUD showErrorWithStatus:@"Something went wrong, \n"
               "please try again!"];
          });

        }

  }];
4

1 回答 1

2

你定义baseURL@"". 可达性块检查baseURL. 您需要通过设置baseURL为实际 URL 来定义一个 URL 以供可达性系统检查。

请注意靠近开头的这段代码-[AFHTTPClient startMonitoringNetworkReachability]

if (!self.baseURL) {
    return;
}

由于未设置基本 URL,因此您的可达性监控永远不会启动。

于 2013-07-26T21:40:00.660 回答