質問

I am using AFNetworking, (AFHTTPRequestOperation) to make calls to the network and get the data back. 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). 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). 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.

1 - Declare a property (alert) of type BlockAlertView in the class that make the multiple requests (let's call it 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

_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:

- (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帰属
所属していません StackOverflow
scroll top