AFNetworking - FOR循环中的AFHTTPRequestOperation(数组枚举)?

时间:2013-08-03 00:16:00

标签: ios objective-c afnetworking afhttprequestoperation

我正在使用AFNetworking(AFHTTPRequestOperation)来调用网络并获取数据。我需要在for循环(卡的枚举)中使用代码来检查每张卡并获取数据,如果操作成功,我得到有关卡的信息,如果失败,我应该得到一个警报(使用警报视图)。问题是,如果失败,我会收到多个警报(因为它在for循环中,并且可能有许多卡)。如何仅在无法连接到网络时显示一个警报?

我知道该操作是异步的,但无法使其正常工作。

以下代码: -

- (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;
 }

它会多次显示警告,如果失败,我只需要显示一次。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您需要将警报视图作为类的属性,所以。

1 - 在发出多个请求的类中声明类型为BlockAlertView的属性(警报)(让我们称之为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 - 修改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;
}

希望它有所帮助!

相关问题