0

Using Socket Rocket library for WebSocket connections.

When I saw the source of Socket Rocket, it is clearly visible that it is calling the methods of its delegate class (my class) asynchronously using dispatch_async.

But when in my class where the delegate the method is implemented, I put a infinite while loop, the UI gets blocked.

Why is that so when the delegate method is already called asynchronously?

SRWebsocket.m

- (void)_handleMessage:(id)message
{
SRFastLog(@"Received message");
[self _performDelegateBlock:^{
    [self.delegate webSocket:self didReceiveMessage:message];
}];
}

// Calls block on delegate queue
- (void)_performDelegateBlock:(dispatch_block_t)block;
{
if (_delegateOperationQueue) {
    [_delegateOperationQueue addOperationWithBlock:block];
} else {
    assert(_delegateDispatchQueue);
    dispatch_async(_delegateDispatchQueue, block);
}
}

My delegate implementation (code that should have been handled asynchronously)

- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {

//some code to process message

//emulating a long running operation
while(1)
{
}


}

But when I put a dispatch_async in my long running operation, the UI doesnt get blocked.

Is it because the block as a whole is run asynchronously but the delegate call inside it is done synchronously. So, as soon as the delegate call is over, _performDelegateBlock returns?

Please explain

kerry
  • 2,362
  • 20
  • 33
  • Are you setting the web socket's dispatch queue or operation queue to anything? By default they are set to the main queue so your delegate methods will be called on the main thread. – dan Jul 02 '15 at 14:35

1 Answers1

2

_delegateDispatchQueue - default value will your main queue. So, if you want to run code asynchronously for your delegate, you need to provide your own queue

Doro
  • 2,413
  • 2
  • 14
  • 26