iOS:AFN网络的可达性
我用这种方法做了一批操作,效果很好iOS:AFN网络的可达性,ios,reachability,afnetworking-2,afhttprequestoperation,Ios,Reachability,Afnetworking 2,Afhttprequestoperation,我用这种方法做了一批操作,效果很好 NSMutableArray *mutableOperations = [NSMutableArray array]; for (NSString *stringURL in url_list) { NSURL *url = [NSURL URLWithString:stringURL]; NSURLRequest *request = [NSURLRequest requestWithURL:url];
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
您正试图从主
NSOperationQueue
获取reachabilityManager
,而主没有。您应该使用[AFNetworkReachabilityManager sharedManager]
获取reachabilityManager
实例
因此:
还考虑尝试暂停主队列的逻辑。您可能想做的是从
AFHTTPRequestOperationManager
实例获取operationQueue
,并挂起该…尝试以下操作:
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN:
case AFNetworkReachabilityStatusReachableViaWiFi:
[[NSOperationQueue mainQueue] setSuspended:NO];
break;
case AFNetworkReachabilityStatusNotReachable:
default:
[[NSOperationQueue mainQueue] setSuspended:YES];
break;
}
}];
您可以将@Simonly的答案简化为:
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
[[NSOperationQueue mainQueue] setSuspended:![AFNetworkReachabilityManager sharedManager].reachable];
}
}];
@Wain是对的,暂停主队列听起来不是一个好计划
使用AFHTTPRequestOperationManager
并分配您自己的操作队列。以及如何获取它?好的,这个“[AFNetworkReachabilityManager sharedManager]”适用于“[NSOperationQueue mainQueue]”?可访问性管理器与操作队列无关。您似乎不太可能尝试挂起主队列……那么我是否应该使用“AFHTTPRequestOperationManager”来正确执行批处理操作?不仅仅是NSOperationQueueok,但我不明白,因为在AFNetworking 2.0中的批处理操作指南中,建议我这样做,而不要使用管理器
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
[[NSOperationQueue mainQueue] setSuspended:![AFNetworkReachabilityManager sharedManager].reachable];
}
}];