setReachabilityStatusChangeBlock

setReachabilityStatusChangeBlock

初始化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!"];
          });

        }

  }];

最佳答案

您正在将baseURL定义为@""。可达性块检查baseURL的可达性的变化。您需要通过将baseURL设置为实际URL来定义可访问性系统要检查的URL。

注意-[AFHTTPClient startMonitoringNetworkReachability]开头附近的这段代码:

if (!self.baseURL) {
    return;
}


由于未设置基本URL,因此永远不会开始进行可达性监视。

关于ios - 具有setReachabilityStatusChangeBlock支持的AFNetworking EnqueueBatchOfHTTPRequestOperations,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17890191/

10-10 21:11