From f18b08a1a4454f8232c2815f0110427c1a86878f Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Wed, 10 Oct 2018 18:55:28 -0700 Subject: [PATCH] Check if optional method of GRPCCallOptions are implemented --- src/objective-c/GRPCClient/GRPCCall.h | 2 +- src/objective-c/GRPCClient/GRPCCall.m | 32 ++++++++++++++++++--------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/objective-c/GRPCClient/GRPCCall.h b/src/objective-c/GRPCClient/GRPCCall.h index 354bdb7cfa2..304fb17cca5 100644 --- a/src/objective-c/GRPCClient/GRPCCall.h +++ b/src/objective-c/GRPCClient/GRPCCall.h @@ -149,7 +149,7 @@ extern id const kGRPCHeadersKey; extern id const kGRPCTrailersKey; /** An object can implement this protocol to receive responses from server from a call. */ -@protocol GRPCResponseHandler +@protocol GRPCResponseHandler @optional diff --git a/src/objective-c/GRPCClient/GRPCCall.m b/src/objective-c/GRPCClient/GRPCCall.m index c9ee5084868..e1039a1b2a7 100644 --- a/src/objective-c/GRPCClient/GRPCCall.m +++ b/src/objective-c/GRPCClient/GRPCCall.m @@ -149,12 +149,16 @@ const char *kCFStreamVarName = "grpc_cfstream"; } if (headers) { dispatch_async(handler.dispatchQueue, ^{ - [handler receivedInitialMetadata:headers]; + if ([handler respondsToSelector:@selector(receivedInitialMetadata:)]) { + [handler receivedInitialMetadata:headers]; + } }); } if (value) { dispatch_async(handler.dispatchQueue, ^{ - [handler receivedMessage:value]; + if ([handler respondsToSelector:@selector(receivedMessage:)]) { + [handler receivedMessage:value]; + } }); } } @@ -171,11 +175,15 @@ const char *kCFStreamVarName = "grpc_cfstream"; } if (headers) { dispatch_async(handler.dispatchQueue, ^{ - [handler receivedInitialMetadata:headers]; + if ([handler respondsToSelector:@selector(receivedInitialMetadata:)]) { + [handler receivedInitialMetadata:headers]; + } }); } dispatch_async(handler.dispatchQueue, ^{ - [handler closedWithTrailingMetadata:self->_call.responseTrailers error:errorOrNil]; + if ([handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) { + [handler closedWithTrailingMetadata:self->_call.responseTrailers error:errorOrNil]; + } }); } }); @@ -193,13 +201,15 @@ const char *kCFStreamVarName = "grpc_cfstream"; if (self->_handler) { id handler = self->_handler; dispatch_async(handler.dispatchQueue, ^{ - [handler closedWithTrailingMetadata:nil - error:[NSError errorWithDomain:kGRPCErrorDomain - code:GRPCErrorCodeCancelled - userInfo:@{ - NSLocalizedDescriptionKey : - @"Canceled by app" - }]]; + if ([handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) { + [handler closedWithTrailingMetadata:nil + error:[NSError errorWithDomain:kGRPCErrorDomain + code:GRPCErrorCodeCancelled + userInfo:@{ + NSLocalizedDescriptionKey : + @"Canceled by app" + }]]; + } }); self->_handler = nil; }