macNewbie macNewbie - 5 months ago 33
Objective-C Question

Using delegate methods with semaphores in Objective C

Hi I am using delegate methods in Objective C and need some help. I have the following functions A and B. B is the delegate function.

-(BOOL) A{
int cmd = 73;
[_serialPort sendData:cmd];
return TRUE;

//Delegate function

-(void) B{
//When some data is received on the serial port function B is called

But function A doesn't wait until data is received on the serial port. How do I block function A until some data is received on the serial port? I am using ORSSerialPort library in objective-C.


It looks like ORSSerialPort already uses a delegate for when you receive data.

- (void)serialPort:(ORSSerialPort *)serialPort didReceiveData:(NSData *)data {
    // Assuming the delegate has method A
    [self A];

-serialPort:didReceiveData: is the delegate method from ORSSerialPort.

The code above will basically wait until data is received, and then once it happened, it will call A.