0

I' doing a 'Batch of Operations' in this way and it work fine

NSMutableArray *mutableOperations = [NSMutableArray array];
    for (NSString *stringURL in url_list) {

        NSURL *url = [NSURL URLWithString:stringURL];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        operation.responseSerializer = [AFHTTPResponseSerializer serializer];
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

            [self addDataToTotal:[self parseJSONfile:responseObject]];

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

        [mutableOperations addObject:operation];
    }

    NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:mutableOperations progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
        NSLog(@"progress:%f", (float)numberOfFinishedOperations / totalNumberOfOperations);
    } completionBlock:^(NSArray *operations) {
        NSLog(@"All operations in batch complete");
        [self startPopulateDBStructure:self.total];
    }];
    [[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];

now I want to use the 'reachability property' to check the connection status and I do this

[[[NSOperationQueue mainQueue]reachabilityManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
                [[NSOperationQueue mainQueue] setSuspended:NO];
                break;
            case AFNetworkReachabilityStatusNotReachable:
            default:
                [[NSOperationQueue mainQueue] setSuspended:YES];
                break;
        }
    }];

but I obtain a crash with this message, where is the problem?

[NSOperationQueue reachabilityManager]: unrecognized selector sent to instance
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241

3 Answers3

1

You are trying to get the reachabilityManager from the main NSOperationQueue, which doesn't have it. You should be using [AFNetworkReachabilityManager sharedManager] to get the reachabilityManager instance.


So:

[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { ...

Also, consider the logic of trying to suspend the main queue. What you probably want to be doing is getting the operationQueue from your AFHTTPRequestOperationManager instance and suspending that...

Wain
  • 118,658
  • 15
  • 128
  • 151
  • and how I can get it? – cyclingIsBetter Feb 26 '14 at 11:18
  • ok and this '[AFNetworkReachabilityManager sharedManager]' work for the '[NSOperationQueue mainQueue]'??? – cyclingIsBetter Feb 26 '14 at 11:20
  • The reachability manager has nothing to do with an operation queue. And it seems unlikely that you should be trying to suspend the main queue... – Wain Feb 26 '14 at 11:22
  • So should I use the 'AFHTTPRequestOperationManager' to do my batch operations right? and not simply the NSOperationQueue – cyclingIsBetter Feb 26 '14 at 11:26
  • ok, but I don't understand because in the guide lines of AFNetworking 2.0 for the batch operations in suggested to do that I do and not to use the manager – cyclingIsBetter Feb 26 '14 at 11:30
  • Ok, but you can't do that if you intend to suspend the queue, you need to use the manager queue or a custom queue you create (and retain). If you were to suspend the main queue the app stops working (from a user point of view). – Wain Feb 26 '14 at 11:32
  • ah ok ok yes you're right. Then I should transform my code for the manager thanks – cyclingIsBetter Feb 26 '14 at 11:34
1

try this:

     [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
                [[NSOperationQueue mainQueue] setSuspended:NO];
                break;
            case AFNetworkReachabilityStatusNotReachable:
            default:
                [[NSOperationQueue mainQueue] setSuspended:YES];
                break;
        }
}];
simalone
  • 2,768
  • 1
  • 15
  • 20
0

You can simplify @simalone's answer to:

[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        [[NSOperationQueue mainQueue] setSuspended:![AFNetworkReachabilityManager sharedManager].reachable];
    }
}];

@Wain is right though, suspending the mainQueue doesn't sound like a great plan.

Use AFHTTPRequestOperationManager and assign your own operationQueue.

Mike Pollard
  • 10,195
  • 2
  • 37
  • 46