如何停止for循环并执行另一种方法然后返回继续objective-c?

时间:2013-02-04 11:48:30

标签: iphone ios objective-c web-services for-loop

我正在使用SOAP WebService并使用for循环来遍历我收到的帐户。

在每个循环中,我调用另一个对WebService的请求,所以我需要的是在for循环中发出请求时,去获取响应然后继续循环。

以下是for循环的代码:

for (A2AWCAccount* account in accountList.Accounts) {
      [_internalAccounts addObject:[[AccountDO alloc] initWithAccountName:[account localizedDescription] AccountNumber:account.Number Balance:[account.CurBal doubleValue] Currency:account.Curr]];

      DataBank* dataBank = [DataBank getInstace];

      A2AService* service = [A2AService service];
      service.logging = YES;

      A2AWCListCard* accountList = [A2AWCListCard alloc];
      accountList.CustMnemonic = dataBank.customerId;
      accountList.SessionID = dataBank.sessionId;
      accountList.AccountNumber = account.Number;
      accountList.Type = @"V";

      // here i make the request
     [service wrListCard:self listCard:accountList];
}

以下是获取响应的方法的代码

- (void) onload:(id)value {
if ([value isKindOfClass:[A2AWCListCard class]]) {
    A2AWCListCard* obj = (A2AWCListCard*) value;
    if (obj.ErrorCode != 0) {
        if (obj.ErrorCode == 109) {
            [self handleSessionError];
            return;
        }else if (obj.ErrorCode == 116)
        {
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Info", @"Info") message:[obj localizedError] delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", @"Ok") otherButtonTitles:nil, nil];
            alert.tag = 5;
            [alert show];
            return;
        }
        [self handleRequestError:[obj localizedError] closeView:YES];
        return;
    }

    for (A2AWCCard* account in obj.Cards) {
        [_cards addObject:[[CardDO alloc] initWithCardNumber:account.CardNo balance:[account.CurBal doubleValue] minimumPayment:account.CardAcc currencyCode:account.Status status:account.Curr cardName:[account localizedDescription] cardNo:account.Number]];
        _totalBalance += [account.CurBal doubleValue];

    }

    [cardsPicker setNeedsDisplay];
    [cardsPicker reloadAllComponents];  

    _servicesInitialized = YES;
}
}

这里发生的问题是循环使其调用N次取决于帐户数量,例如,如果有3个帐户,它会发出3个请求,然后我等待3个响应,因此它有时会因为服务器而互相贡献有点慢。

有人帮忙吗?

1 个答案:

答案 0 :(得分:1)

我同意这些评论......你有一个架构问题。

不要这样做:

You are trying to do what I depicted here:

而是这样做:

What you should do is like so:

1首先,您知道有多少帐户,所以请保留该信息。

2创建一个队列并将其限制为让我们说3个操作

3观察您的队列以了解处理完计数的时间

4现在循环您的帐户,将操作添加到您的队列

注意:您可以使用GCD,但这更适合您需要做的事情

5您可以将NSOperation子类化以将信息返回到主队列

注意:您可以将此方法与NSURLConnection一起使用

(sendAsynchronousRequest:队列:completionHandler:)

6在观察队列的函数中,当你点击计数时......

A Kill the queue

B Do something with the returned data, probably in some array
祝你好运!