等待回调函数在iOS / iPhone中返回

时间:2012-03-29 07:50:39

标签: iphone ios asynchronous callback

我需要在我的应用中拨打电话getValuesAndCalculate,该电话应该在完成工作后返回。但是,为了完成它的工作,它需要从服务器获取记录,这必须通过异步调用来完成。通过回调函数接收服务器数据。因此,在getValuesAndCalculate中,我需要等到我有数据才能继续计算。我该如何实现呢?

3 个答案:

答案 0 :(得分:2)

使用协议和代理:

委托只不过是将类中的对象分配给服务器端的对象。 服务器可以使用此对象在客户端代码上调用方法。

答案 1 :(得分:0)

尝试使用NSRunloop,直到从服务器获取数据。

例如:

while(!isFinished)   {     [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:1.0]   }

答案 2 :(得分:0)

你可以为这个问题实现线程,即

// in main thread
// start activity indicator
NSThread *calculateThread = [[NSThread alloc] initWithTarget:self selector:@selector(getValuesAndCalculate) object:nil];
[calculateThread start];
// End of Main thread



- (void)getValuesAndCalculate
{
    // Perform transactions with server 
        [self performSelectorOnMainThread:@selector(continueMainThreadOperations) withObject:nil waitUntilDone:YES];
}

多数民众赞成!