gpt4 book ai didi

iOS:AFNetworking 的可达性

转载 作者:行者123 更新时间:2023-11-28 22:14:33 25 4
gpt4 key购买 nike

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

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 实例。


所以:

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

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

关于iOS:AFNetworking 的可达性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22039605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com