我正在以这种方式进行“批量操作”,并且效果很好

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];


现在,我想使用“可达性属性”来检查连接状态,然后执行此操作

[[[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;
        }
    }];


但我收到此消息后崩溃,问题出在哪里?

[NSOperationQueue reachabilityManager]: unrecognized selector sent to instance

最佳答案

您正在尝试从主reachabilityManager中获取它,而该主NSOperationQueue没有它。您应该使用[AFNetworkReachabilityManager sharedManager]来获取reachabilityManager实例。



所以:

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




另外,请考虑尝试挂起主队列的逻辑。您可能想做的是从operationQueue实例获取AFHTTPRequestOperationManager并将其挂起...

08-05 23:45