我有一个基本上调用Web服务的for循环。响应在完成处理程序
中处理除非对所有请求都有响应,否则我不希望执行从此for循环中出来。
下面是我的代码段
for(ClassX objectX in myAraay)
{
__block BOOL blockExecutionOver = NO ;
// call web service with completion handeler
callwebservice:^handler
{
// block execution
blockExecutionOver = YES ;
}
];
while (blockExecutionOver == NO)
{
[[NSRunLoop currentRunLoop] run];
}
}
//do something here after above for loop is executed
我如何实现这一点。目前,这个运行循环对我没有任何意义。我对这些请求没有超时。因此我没有使用runtillDate或runMode
答案 0 :(得分:1)
您可以使用NSCondition
来实现这种行为。 E.g:
self.condition = [[NSCondition alloc] init];
[self.condition lock];
self.requestCount = 0;
for (...) {
...
self.requestCount++;
...
}
if (self.requestCount > 0)
[self.condition wait];
[self.condition unlock];
然后在你的完成块中你会这样做:
... process response ...
self.requestCount--;
if (self.requestCount == 0)
[self.condition signal];
上述代码的作用是:
还可以采用其他方式,即使用NSOperationQueue
,但这需要对代码进行更多重构。 Here你可以找到一个如何做到这一点的例子。
答案 1 :(得分:0)
所以你要锁定它,直到调用完成:
dispatch_queue_t q = dispatch_queue_create("com.my.queue", NULL);
dispatch_sync(q, ^{
// Do your work here.
});