gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:56:52 24 4
gpt4 key购买 nike

我使用的是最新的 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/

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