macNewbie macNewbie - 1 year ago 75
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download