2013-02-04 44 views
0

我正在使用SOAP WebService,并使用for循环来循环接收的帐户。如何停止for循环并执行另一个方法,然后返回以继续使用objective-c?

在每一个循环中,我调用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次响应,所以它有助于彼此,有时是因为服务器有点慢。

请帮助吗?

+0

这些调用是在主线程上执行的吗? – trojanfoe

+0

我不知道,但我认为是它的主线程,因为它没有设置为任何其他线程 –

+0

不会阻止主(UI)线程很长一段时间? '[服务wrListCard:self listCard:accountList];'如何实现? – trojanfoe

回答

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 

祝你好运!

+0

我真的很感谢你的工作,对于NSOperationQueue有点新鲜,所以我初始化它并设置最大数量,但之后我不知道该怎么办,代码是上面可以请你帮我一下! –

+0

我提到了以下方法(sendAsynchronousRequest:queue:completionHandler :),因为第二个参数将采用您创建的队列。一起玩吧,一旦你使用它并没有那么糟糕。噢,和块一样......一旦你掌握了它们,你就会爱上它们。 – Arvin

+0

好的,我会尽力让它通知你,谢谢 –

相关问题