From 5d16c2ff92eb49319d9028cbe99bb80bf41e0578 Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Thu, 11 Oct 2018 11:20:26 -0700 Subject: [PATCH] Move issuance of response in helper functions --- src/objective-c/GRPCClient/GRPCCall.m | 54 ++++++++++++++++----------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/src/objective-c/GRPCClient/GRPCCall.m b/src/objective-c/GRPCClient/GRPCCall.m index e1039a1b2a7..1e9bc41b41b 100644 --- a/src/objective-c/GRPCClient/GRPCCall.m +++ b/src/objective-c/GRPCClient/GRPCCall.m @@ -141,25 +141,16 @@ const char *kCFStreamVarName = "grpc_cfstream"; id responseWriteable = [[GRXWriteable alloc] initWithValueHandler:^(id value) { dispatch_async(self->_dispatchQueue, ^{ if (self->_handler) { - id handler = self->_handler; NSDictionary *headers = nil; if (!self->_initialMetadataPublished) { headers = self->_call.responseHeaders; self->_initialMetadataPublished = YES; } if (headers) { - dispatch_async(handler.dispatchQueue, ^{ - if ([handler respondsToSelector:@selector(receivedInitialMetadata:)]) { - [handler receivedInitialMetadata:headers]; - } - }); + [self issueInitialMetadata:headers]; } if (value) { - dispatch_async(handler.dispatchQueue, ^{ - if ([handler respondsToSelector:@selector(receivedMessage:)]) { - [handler receivedMessage:value]; - } - }); + [self issueMessage:value]; } } }); @@ -167,24 +158,15 @@ const char *kCFStreamVarName = "grpc_cfstream"; completionHandler:^(NSError *errorOrNil) { dispatch_async(self->_dispatchQueue, ^{ if (self->_handler) { - id handler = self->_handler; NSDictionary *headers = nil; if (!self->_initialMetadataPublished) { headers = self->_call.responseHeaders; self->_initialMetadataPublished = YES; } if (headers) { - dispatch_async(handler.dispatchQueue, ^{ - if ([handler respondsToSelector:@selector(receivedInitialMetadata:)]) { - [handler receivedInitialMetadata:headers]; - } - }); + [self issueInitialMetadata:headers]; } - dispatch_async(handler.dispatchQueue, ^{ - if ([handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) { - [handler closedWithTrailingMetadata:self->_call.responseTrailers error:errorOrNil]; - } - }); + [self issueClosedWithTrailingMetadata:self->_call.responseTrailers error:errorOrNil]; } }); }]; @@ -230,6 +212,34 @@ const char *kCFStreamVarName = "grpc_cfstream"; }); } +- (void)issueInitialMetadata:(NSDictionary *)initialMetadata { + id handler = self->_handler; + if ([handler respondsToSelector:@selector(receivedInitialMetadata:)]) { + dispatch_async(handler.dispatchQueue, ^{ + [handler receivedInitialMetadata:initialMetadata]; + }); + } +} + +- (void)issueMessage:(id)message { + id handler = self->_handler; + if ([handler respondsToSelector:@selector(receivedMessage:)]) { + dispatch_async(handler.dispatchQueue, ^{ + [handler receivedMessage:message]; + }); + } +} + +- (void)issueClosedWithTrailingMetadata:(NSDictionary *)trailingMetadata + error:(NSError *)error { + id handler = self->_handler; + if ([handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) { + dispatch_async(handler.dispatchQueue, ^{ + [handler closedWithTrailingMetadata:self->_call.responseTrailers error:error]; + }); + } +} + @end // The following methods of a C gRPC call object aren't reentrant, and thus