具有 setReachabilityStatusChangeBlock 支持的 AFNetworking EnqueueBatchOfHTTPRequestOperations

Posted

技术标签:

【中文标题】具有 setReachabilityStatusChangeBlock 支持的 AFNetworking EnqueueBatchOfHTTPRequestOperations【英文标题】:AFNetworking EnqueueBatchOfHTTPRequestOperations with setReachabilityStatusChangeBlock support 【发布时间】:2013-07-26 20:10:56 【问题描述】:

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

        

  ];

【问题讨论】:

你如何测试你的可达性块没有被调用? 一旦包含这两个块的主函数被调用,我在那里有多个断点,我想这也必须是因为在 enqueueBatchOfHTTPRequestOperations:requestsForDownload 处理所有请求 baseURL 应该在 sharedClient 中定义 - 你应该调用 initWithBaseURL: 另外,如果您调用的是 init 而不是 initWithBaseURL:,并且您没有遇到异常,那么您使用的是旧版本的 AFNetworking。 是的,但是因为每个 requestForDownload 都有自己的 URL,我无法设置 baseURL 【参考方案1】:

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

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

if (!self.baseURL) 
    return;

您的可达性监控从未启动,因为未设置基本 URL。

【讨论】:

您说得对,非常感谢您的帮助,只需测试一下,效果很好。

以上是关于具有 setReachabilityStatusChangeBlock 支持的 AFNetworking EnqueueBatchOfHTTPRequestOperations的主要内容,如果未能解决你的问题,请参考以下文章

具有透明背景的 UITableView 具有纯黑色边框

编写一个Car类,具有String类型的属性品牌,具有功能drive;

具有多个视图的 UIScrollView(具有按钮、滚动视图和标签)

如何使用 ZIO(具有持久层)创建具有“传统”接口的小型库

sharepoint 是不是具有逐项权限,就像它具有逐项权限一样

Java - 无法创建具有 777 权限的目录(改为具有 775)[重复]