[英]AFNetworking - AFHTTPRequestOperation in a for loop (array enumeration)?
I am using AFNetworking, (AFHTTPRequestOperation) to make calls to the network and get the data back. 我正在使用AFNetworking(AFHTTPRequestOperation)进行网络调用并取回数据。 I need to make use of the code in a for loop (enumeration of cards) to check for each card and get the data back, if the operation is successful, I get the information about the cards and if it fails, I should get an alert (using an alert view).
我需要使用for循环中的代码(卡的枚举)检查每个卡并获取数据,如果操作成功,我将获得有关卡的信息,如果失败,则应获取警报(使用警报视图)。 The problem is I am getting multiple alerts if it fails (because it's inside a for loop and there can be a number of cards).
问题是如果失败,我会收到多个警报(因为它位于for循环内,并且可能有许多卡)。 How can I just show one alert only when it fails to connect to the network?
仅当警报无法连接到网络时,我如何才能显示它?
I know the operation is async, but can't get this to work. 我知道操作是异步的,但无法正常工作。
Code below:- 代码如下:
- (void)verifyMobileDeviceStatus
{
[self fetchRequest];
[self.contentsArray enumerateObjectsUsingBlock:^(GCards *gCard, NSUInteger idx, BOOL * stop) {
NSURL *baseURL = nil;
baseURL = [NSURL URLWithString:BASE_URL_STRING];
NSString *soapBody = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><soapenv:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header/><soapenv:Body><VerifyMobileDeviceStatus xmlns=\"http://tempuri.org/\"><Request><AuthToken>%@</AuthToken></Request></VerifyMobileDeviceStatus></soapenv:Body></soapenv:Envelope>", [gCard valueForKey:@"authToken"]];
NSLog(@" auth token =%@", [gCard valueForKey:@"authToken"]);
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:baseURL];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapBody length]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];
[request addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[request addValue:@"http://tempuri.org/VerifyMobileDeviceStatus" forHTTPHeaderField:@"SOAPAction"];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success: %@", operation.responseString);
NSString *xmlString = [operation responseString];
[parser setGCard:gCard];
[parser parseXML:xmlString];
if([gCard.merchantStatus isEqualToString:MERCHANT_STATUS_ACTIVE])
{
gCard.isPremiumAccount = [NSNumber numberWithInt:1];
}
else
{
gCard.isPremiumAccount = [NSNumber numberWithInt:0];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[parser.delegate verifyDeviceStatusParserDidFailWithError:@"Error"];
NSLog(@"error: %@", [error userInfo]);
}];
[operation start];
}];
}
- (void)verifyDeviceStatusParserDidFailWithError:(NSString *)error
{
NSString *errorString = [NSString stringWithFormat:@"Error =%@", [error description]];
NSLog(@"Error parsing XML: %@", errorString);
BlockAlertView* alert = [BlockAlertView alertWithTitle:@"Connection Failed" message:@"Connection to web service Failed. Please try again."];
[alert addButtonWithTitle:NSLocalizedString(@"OK", nil) block:^{ }];
[alert show];
[activityIndicator stopAnimating];
self.navigationController.view.userInteractionEnabled = YES;
}
It's showing the alert multiple times, if it fails and I need to show it only once. 它会多次显示警报,如果警报失败,则只需要显示一次。
Any help would be appreciated. 任何帮助,将不胜感激。
You need to make the alert view a property of the class, so. 因此,您需要使Alert视图成为该类的属性。
1 - Declare a property (alert) of type BlockAlertView
in the class that make the multiple requests (let's call it RequesterClass). 1-在发出多个请求的类中声明一个
BlockAlertView
类型的属性(警报)(我们将其称为RequesterClass)。 This property will reference an unique alert view, which will be displayed only once. 此属性将引用唯一的警报视图,该视图仅显示一次。
2 - Put this 2 lines in the init method of the RequesterClass 2-将这2行放入RequesterClass的init方法中
_alert = [BlockAlertView alertWithTitle:@"Connection Failed" message:@"Connection to web service Failed. Please try again."];
[_alert addButtonWithTitle:NSLocalizedString(@"OK", nil) block:^{ }];
3 - Modify the verifyDeviceStatusParserDidFailWithError:
as follows: 3-修改
verifyDeviceStatusParserDidFailWithError:
,如下所示:
- (void)verifyDeviceStatusParserDidFailWithError:(NSString *)error
{
NSString *errorString = [NSString stringWithFormat:@"Error =%@", [error description]];
NSLog(@"Error parsing XML: %@", errorString);
if(!alert.visible)
{
[alert show];
}
[activityIndicator stopAnimating];
self.navigationController.view.userInteractionEnabled = YES;
}
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.