nsoperation - 即使操作队列被挂起,AFHTTPRequestOperation 仍在运行

标签 nsoperation nsoperationqueue reachability afnetworking-2

我使用的是最新的 AFNetworking V2.0。如果可达性可用,我已经设置了一系列要在 AFHTTPRequestOperationManager 的操作队列上运行的操作。我在 appDelegate 中设置了一个全局 AFHTTPRequestOperationManager 并使用它来检查可达性,当可达性不可用时,我暂停 AFHTTPRequestOperationManager 的操作队列以避免运行操作。当可达性恢复后,我将 isSuspended 设置为 false 以重新启动操作。

我现在遇到的问题是,即使我在可达性不可用时将 isSuspended 设置为 YES,操作队列仍然会触发并转到操作的失败 block 。

这是我的设置,

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

如何在没有网络的情况下暂停操作队列?并在互联网恢复时重新启动队列?

谢谢, 安东尼

最佳答案

如果有人感兴趣,我已经用最终代码编辑了我以前的代码。

关于nsoperation - 即使操作队列被挂起,AFHTTPRequestOperation 仍在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19499126/

相关文章:

ios - 核心数据/NSOperation : crash while enumerating through and deleting objects

ios - 访问 NSOperation 上的属性

ios - NSOperationQueue 不限制并发操作

iphone - 使用 GCD 完成 block

ios - ReachabilitySwift 有什么问题?

ios - 在 iOS 7 上获取互联网/网络的状态

iphone - 在 iOS4 设备上后台发送大量数据的最佳实践?

objective-c - NSOperationQueue 异步下载网页

iphone - NSOperation 中的异步方法

ios - 等待进入 Controller ,直到 wifi 启用 IOS