Iphone 如何停止for循环并执行另一种方法,然后返回继续使用objective-c?
我使用soapwebservice,并使用for循环来循环我收到的帐户 在每个循环中,我调用另一个对Web服务的请求,因此我需要的是在for循环中,当发出请求时,获取响应,然后继续循环 下面是for循环的代码:Iphone 如何停止for循环并执行另一种方法,然后返回继续使用objective-c?,iphone,ios,objective-c,web-services,for-loop,Iphone,Ios,Objective C,Web Services,For Loop,我使用soapwebservice,并使用for循环来循环我收到的帐户 在每个循环中,我调用另一个对Web服务的请求,因此我需要的是在for循环中,当发出请求时,获取响应,然后继续循环 下面是for循环的代码: for (A2AWCAccount* account in accountList.Accounts) { [_internalAccounts addObject:[[AccountDO alloc] initWithAccountName:[account localiz
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因此,首先你知道有多少帐户,所以请保留这些信息 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
祝你好运 这些调用是在主线程上执行的吗?我不知道,但我认为是的,它是在主线程上执行的,因为它没有设置为任何其他线程,这不会长时间阻止主(UI)线程吗?How is
[服务列表卡:自助列表卡:账户列表]代码>实现?不,它不会在一段时间内阻止它,这个方法很难在这里说明它是如何实现的。我使用了sudzC.com,它会自动为我生成Web服务,它会使一切顺利。只要在后台发出请求,你就可以了;仅仅因为它在开发过程中不会阻塞(服务器通常与客户端位于同一网络上),并不意味着它不会阻塞“在野外”(客户端通过3G连接到互联网)。了解这些方法如何提供反馈对于回答您的问题至关重要;听起来您需要将请求的当前“状态”输入执行
for循环的方法中。我非常感谢您的工作,我对NSOperationQueue有点陌生,所以我初始化了它并设置了最大数量,但之后我不知道该做什么,代码在上面,请您帮我处理好吗!我提到了以下方法(sendAsynchronousRequest:queue:completionHandler:),因为第二个参数将采用您创建的队列。玩玩它,一旦你用了它就没那么糟糕了。哦,是的,积木也是如此……一旦你掌握了窍门,你就会爱上它们。