2013-07-26 85 views
0

我有一個setReachabilityStatusChangeBlock代碼塊初始化一個AFHTTPClient的共享實例後,然後我有一個enqueueBatchOfHTTPRequestOperations。問題是setReachabilityStatusChangeBlock永遠不會被執行,我試圖捕捉可能危及在enqueueBatchOfHTTPRequestOperations中下載的任何文件的可憐的網絡連接。AFNetworking EnqueueBatchOfHTTPRequestOperations與setReachabilityStatusChangeBlock支持

任何與此有關的幫助將非常感激。

這是什麼,我有一個例子...

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

     } 

    }]; 
+0

你如何測試你的可達性塊沒有被調用? –

+0

一旦包含這兩個塊的主函數被調用,我有多個breackpoints,我想它也必須與這樣一個事實,即baseURL沒有被定義,直到enqueueBatchOfHTTPRequestOperations:requestsForDownload處理所有請求 –

+0

'baseURL'應該是在'sharedClient'中定義 - 你應該調用'initWithBaseURL:'。 –

回答

2

你定義baseURL@""。可達性塊檢查baseURL的可達性變化。您需要通過將baseURL設置爲實際URL來爲可訪問性系統定義一個URL來檢查。

注意此代碼附近的-[AFHTTPClient startMonitoringNetworkReachability]開頭:

if (!self.baseURL) { 
    return; 
} 

你的可達性監測從來沒有開始,因爲基本URL未設置。

+0

你是對的,非常感謝你的幫助,只是測試它,並且效果很好。 –