gigahari
gigahari

Reputation: 681

Waiting for callback functions to return in iOS/iPhone

I need to make a call getValuesAndCalculate in my app, which should return only after doing its work. However, for doing its work, it needs to get records from a server, which has to be done through an async call. The server data is received through a callback function. Thus, in getValuesAndCalculate I need to wait till I have the data before proceeding with the calculations. How do I implement this?

Upvotes: 0

Views: 1368

Answers (3)

Shiva Kumar Ganthi
Shiva Kumar Ganthi

Reputation: 426

You can implement Threads for this question i.e,

// 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];
}

Thats it!

Upvotes: 0

arun.s
arun.s

Reputation: 1528

Try using NSRunloop till you get the data from the server.

For eg :

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

Upvotes: 0

Aatish Molasi
Aatish Molasi

Reputation: 2186

Use protocols and delegates :

delegates are nothing but you assigning an object from your class to a object on the server side . The server can use this object to call a method on your client side code.

Upvotes: 2

Related Questions