0

Assume that in my SocketNetwork.h i have

[self.delegate receivedResponseFromServer:responseDistance];

That mean, we i got a response from socket server after i had a request -> i will raise an event to let ViewController know about this.

In my view controller (ViewController.h) i have

@interface ViewController: UIViewController <SocketNetworkDelegate>

ViewController.m:

-(float)calculateDistance{
    [SocketNetwork requestDistance:(point *A, point *B)];
    //here: how to wait util self.distance has a value -> we return / if not, we still wait.

    return self.distance;
}

//socketNetwork delegate
-(void)receivedResponseFromServer:responseDistance{
    self.distance = responseDistance;
}

The above code is only an example to let everybody easy to understand my situation. In this above code. How could i wait util i receive responseDistance from SocketNetworking? After that, i return self.distance???

Thanks in advance.

Jonny Vu
  • 1,420
  • 2
  • 13
  • 32
  • It's not clear what you want to do here. Where do you want to return self.distance to? If you're calling `responseRecievedFromServer` as a delegate method from `socketNetwork`, then you shouldn't have to wait as you'll only send that message after you receive `responseDistance`. – Josh Heald Nov 07 '14 at 08:34
  • Because of I am using socket with 2 thread, one for OutputStream and one for InputStream. It is not look like Http Request with JSON. – Jonny Vu Nov 07 '14 at 08:41
  • Use `completion handler` instead of delegate. – iphonic Nov 07 '14 at 09:00
  • @iphonic If i do not using delegate, how could i know when it completed – Jonny Vu Nov 07 '14 at 09:02
  • dispatch your requestDistance method in another thread and put the call thread in wait until you receive the response from server using delegate method. To put thread on wait you can user while loop with a bool value and change that value from delegate response to break the loop. Hope it helps. If any explanation required then feel free to ask. – if-else-switch Nov 07 '14 at 09:03
  • When debug i founded that, if i use `while(true)`, it take 100% cpu. Is it ok if i still using it? – Jonny Vu Nov 07 '14 at 09:10

1 Answers1

0

You can use dispatch_semaphore_t for that purpose.

So dispatch_semaphore_wait will wait until dispatch_semaphore_signal will be triggered.

Here is a Basic implementation which will block your main thread if you will use it like that

@interface ViewController ()

@property (nonatomic) dispatch_semaphore_t semaphore;

@end

@implementation ViewController

- (float)calculateDistance{

    self.semaphore = dispatch_semaphore_create(0);
    [SocketNetwork requestDistance:(point *A, point *B)];
    //here: how to wait util self.distance has a value -> we return / if not, we still wait.
    dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
    return self.distance;
}

//socketNetwork delegate
- (void)receivedResponseFromServer:responseDistance{
    self.distance = responseDistance;
    dispatch_semaphore_signal(semaphore);
}  

Here is nice implementation that I advice you to use (using blocks)

typedef void (^DistanceCalculationCallback) (float distance);

@interface ViewController ()

@property (copy, nonatomic) DistanceCalculationCallback distanceCallback;

@end

@implementation ViewController

//--------------------------------------------//
#pragma mark - Public Methods
//--------------------------------------------//

- (void)calculateDistanceWithCompletion:(DistanceCalculationCallback)aCallback
{
    self.distanceCallback = aCallback;
    [SocketNetwork requestDistance:(point *A, point *B)];
}

//--------------------------------------------//
#pragma mark - SocketNetwork delegate
//--------------------------------------------//

- (void)receivedResponseFromServer:(float)responseDistance
{
    if (self.distanceCallback) {
        self.distanceCallback(responseDistance);
    }
}

//--------------------------------------------//
#pragma mark - LifeCycle
//--------------------------------------------//

- (void)viewDidLoad
{
    [super viewDidLoad];
    // We will load distance here for exampple
    [self calculateDistanceWithCompletion:^(float distance) {
        NSLog(@"Distance is %f", distance);
    }];
}
l0gg3r
  • 8,864
  • 3
  • 26
  • 46