Question

I'm using the latest AFNetworking V2.0. I have setup series of operations to be run on the AFHTTPRequestOperationManager's operation queue if the reachability is available. I have setup a global AFHTTPRequestOperationManager in the appDelegate and uses that to check the reachability and when the reachability is not available i suspend the AFHTTPRequestOperationManager's operation queue in order to avoid running the operations. When the reachability is back i make the isSuspended to false to restart the operations.

The problem i'm having now is even i have set the isSuspended to YES when the reachability is not available, the operation queue still fires and goes to the failure block of the operation.

This is my setup,

AppDelegate.m

self.reachManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:API_SERVER_URL]];
self.reachManager.responseSerializer = [AFJSONResponseSerializer serializer];
__block NSOperationQueue *operationQueue = self.reachManager.operationQueue;
[operationQueue setMaxConcurrentOperationCount:1];

__block AppDelegate * weakSelf = self;

[self.reachManager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    switch (status) {
        case AFNetworkReachabilityStatusNotReachable:
            NSLog(@"not reachable");
            [operationQueue setSuspended:YES];
            [weakSelf showReachabilityAlert:YES];

            break;
        case AFNetworkReachabilityStatusReachableViaWiFi:
            [weakSelf showReachabilityAlert:NO];
            [operationQueue setSuspended:NO];
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:
            [weakSelf showReachabilityAlert:NO];
            [operationQueue setSuspended:NO];
            break;
        default:
            [weakSelf showReachabilityAlert:NO];
            [operationQueue setSuspended:YES];
            break;
    }
}];

[self.reachManager.reachabilityManager startMonitoring];

View Controller

AppDelegate *ad = [[UIApplication sharedApplication] delegate];
NSMutableArray *mutableOperations = [NSMutableArray array];
NSArray * data = [NSArray arrayWithObjects:@"media", @"analysis", nil];

for(NSString * c in data)
{
    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:API_SERVER_SECTION_URL, c]];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.responseSerializer = [AFJSONResponseSerializer serializer];

    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

            NSLog(@"JSON: received for tag %@", c);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error message : %@", error);
    }];

    [mutableOperations addObject:op];
}

NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:mutableOperations progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
    NSLog(@"%d of %d complete", numberOfFinishedOperations, totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
    NSLog(@"All operations in batch complete");


}];

//Check the reachability and suspend the opearation queue if there is no reachability
if([ad.reachManager.reachabilityManager isReachable])
    [ad.reachManager.operationQueue setSuspended:NO];
else
    [ad.reachManager.operationQueue setSuspended:YES];

[ad.reachManager.operationQueue addOperations:operations waitUntilFinished:YES];

How can I suspend the operation queue when the internet is not there? and restart the queue when the internet is back?

Thanks, Anthony

Was it helpful?

Solution

I have edited my previous code with the final code if someone interested.

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