Question

I have a setReachabilityStatusChangeBlock block of code after initialize a shared instance of an AFHTTPClient, then I have a enqueueBatchOfHTTPRequestOperations. The problem is that the setReachabilityStatusChangeBlock never get executed, I'm trying to catch poor network connections that can compromise any file being downloaded in the enqueueBatchOfHTTPRequestOperations.

Any help with this will be really appreciate it.

this is an example of what I have...

////////////////////////
// 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!"];
          });

        }

  }];
Was it helpful?

Solution

You're defining baseURL as @"". The reachability block checks for changes in the reachability of the baseURL. You need to define a URL for the reachability system to check, by setting baseURL to an actual URL.

Note this code near the beginning of -[AFHTTPClient startMonitoringNetworkReachability]:

if (!self.baseURL) {
    return;
}

Your reachability monitoring is never started because a base URL isn't set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top