DyKnow / SignalR-ObjC

Objective-C Client for the SignalR Project works with iOS and Mac
MIT License
442 stars 213 forks source link

Invoking server method from hub - completionHandler is calling twice #275

Open galoz opened 8 years ago

galoz commented 8 years ago

When calling to server method from client (hub connection, long polling) the call back block is being called twice. For example below code open hub connection and call 'ping' method on server hub, the response completedHandler is being called twice, so the log looks like this

2016-07-20 10:22:18.573 MyApp[8590:406067] requestURL response pong 2016-07-20 10:22:18.573 MyApp[8590:406067] requestURL response { I = 13; R = pong; }

//Server
public class CometHub : Hub 
{
    public string Ping()
    {
          return "pong";
    }
}

//Client
-(void)testPing{
    [self startSignalRConnection];
    [self requestURL:@"ping"];
}

-(void)startSignalRConnection{
   SRHubConnection *hubConnection = [SRHubConnection connectionWithURLString:SERVER_HOST_URL];
   // Create a proxy to the cometHub service
   SRHubProxy *cometHub = [hubConnection createHubProxy:@"cometHub"];
   // Register for connection lifecycle events
   [hubConnection setStarted:^{self.myCometHub = cometHub;}];
   [hubConnection setClosed:^{self.myCometHub = nil;}];
   [hubConnection start:[[SRLongPollingTransport alloc] init]];//start long pooling 
}

-(void) requestURL:(NSString *)url {
    if (self.myCometHub == nil) 
         NSLog(@"myCometHub is not connected");
    **[self.myCometHub invoke:url withArgs:@[] completionHandler:^(id response, NSError *error) {**
    if (error != nil){
         NSLog(@"requestURL Error %@",error);
    } else {
         NSLog(@"requestURL response %@",response);
    **}**
 }];
}

Short debugging of the issue expose that SRHttpBasedTransport is calling to the call back twice 1)trough SRHubConnection which execute SRHubProxy callback (callBack with the result) 2) directly (callback with server raw response)

- (void)send:(id<SRConnectionInterface>)connection data:(NSString *)data connectionData:(NSString *)connectionData completionHandler:(void (^)(id response, NSError *error))block;
...
[connection didReceiveData:responseObject]; //call SRHubConnection which execute SRHubProxy callback (which call the original call block(result.result, nil); 
        if(block) {
            block(responseObject, nil);//call with the full response object to the block
        }
...

The solution should disable the second call to completionHandler (the one with the raw response). I'm afraid that deleting "block(responseObject, nil)" in SRHttpBasedTransport will cause other issue. As this is critical bug for my opinion, please advise Thanks Gal

galoz commented 8 years ago

same as https://github.com/DyKnow/SignalR-ObjC/issues/161#issuecomment-233408201 You can close this one as duplicate and re-open the original one. The issue happens on the current cocoa pods version