From 38d361c3988ea2e0b4bf3935ccbc72b485ae8041 Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Wed, 27 Nov 2019 10:22:04 -0800 Subject: [PATCH 1/7] Update GRPCUnaryResponseHandler with generics --- .../objective-c/route_guide/ViewControllers.m | 19 ++++++++++++++++++- src/objective-c/ProtoRPC/ProtoRPC.h | 4 ++-- src/objective-c/ProtoRPC/ProtoRPC.m | 4 ++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/examples/objective-c/route_guide/ViewControllers.m b/examples/objective-c/route_guide/ViewControllers.m index 7033cb28f78..b1d49c3b64a 100644 --- a/examples/objective-c/route_guide/ViewControllers.m +++ b/examples/objective-c/route_guide/ViewControllers.m @@ -106,6 +106,23 @@ static NSString * const kHostAddress = @"localhost:50051"; } - (void)execRequest { + void (^handler)(RTGFeature *response, NSError *error) = ^(RTGFeature *response, NSError *error) { + // TODO(makdharma): Remove boilerplate by consolidating into one log function. + if (response.name.length) { + NSString *str =[NSString stringWithFormat:@"%@\nFound feature called %@ at %@.", self.outputLabel.text, response.location, response.name]; + self.outputLabel.text = str; + NSLog(@"Found feature called %@ at %@.", response.name, response.location); + } else if (response) { + NSString *str =[NSString stringWithFormat:@"%@\nFound no features at %@", self.outputLabel.text,response.location]; + self.outputLabel.text = str; + NSLog(@"Found no features at %@", response.location); + } else { + NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; + self.outputLabel.text = str; + NSLog(@"RPC error: %@", error); + } + }; + RTGPoint *point = [RTGPoint message]; point.latitude = 409146138; point.longitude = -746188906; @@ -115,7 +132,7 @@ static NSString * const kHostAddress = @"localhost:50051"; callOptions:nil]; [call start]; call = [_service getFeatureWithMessage:[RTGPoint message] - responseHandler:self + responseHandler:[[GRPCUnaryResponseHandler alloc] initWithResponseHandler:handler responseDispatchQueue:nil] callOptions:nil]; [call start]; diff --git a/src/objective-c/ProtoRPC/ProtoRPC.h b/src/objective-c/ProtoRPC/ProtoRPC.h index 778c421991d..7f0e68fbea0 100644 --- a/src/objective-c/ProtoRPC/ProtoRPC.h +++ b/src/objective-c/ProtoRPC/ProtoRPC.h @@ -74,7 +74,7 @@ NS_ASSUME_NONNULL_BEGIN * A convenience class of objects that act as response handlers of calls. Issues * response to a single handler when the response is completed. */ -@interface GRPCUnaryResponseHandler : NSObject +@interface GRPCUnaryResponseHandler : NSObject /** * Creates a responsehandler object with a unary call handler. @@ -83,7 +83,7 @@ NS_ASSUME_NONNULL_BEGIN * responseDispatchQueue: the dispatch queue on which the response handler * should be issued. If it's nil, the handler will use the main queue. */ -- (nullable instancetype)initWithResponseHandler:(void (^)(GPBMessage *, NSError *))handler +- (nullable instancetype)initWithResponseHandler:(void (^)(ResponseType, NSError *))handler responseDispatchQueue:(nullable dispatch_queue_t)dispatchQueue; /** Response headers received during the call. */ diff --git a/src/objective-c/ProtoRPC/ProtoRPC.m b/src/objective-c/ProtoRPC/ProtoRPC.m index 3d0c4ae283a..d140d4a75e4 100644 --- a/src/objective-c/ProtoRPC/ProtoRPC.m +++ b/src/objective-c/ProtoRPC/ProtoRPC.m @@ -28,13 +28,13 @@ #import @implementation GRPCUnaryResponseHandler { - void (^_responseHandler)(GPBMessage *, NSError *); + void (^_responseHandler)(id, NSError *); dispatch_queue_t _responseDispatchQueue; GPBMessage *_message; } -- (nullable instancetype)initWithResponseHandler:(void (^)(GPBMessage *, NSError *))handler +- (nullable instancetype)initWithResponseHandler:(void (^)(id, NSError *))handler responseDispatchQueue:(dispatch_queue_t)dispatchQueue { if ((self = [super init])) { _responseHandler = handler; From 91cc01e541dc93186ec7a26d6d37a5900d8b1333 Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Wed, 27 Nov 2019 10:38:31 -0800 Subject: [PATCH 2/7] nit fix --- examples/objective-c/route_guide/ViewControllers.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/objective-c/route_guide/ViewControllers.m b/examples/objective-c/route_guide/ViewControllers.m index b1d49c3b64a..deddc05699d 100644 --- a/examples/objective-c/route_guide/ViewControllers.m +++ b/examples/objective-c/route_guide/ViewControllers.m @@ -128,7 +128,7 @@ static NSString * const kHostAddress = @"localhost:50051"; point.longitude = -746188906; GRPCUnaryProtoCall *call = [_service getFeatureWithMessage:point - responseHandler:self + responseHandler:[[GRPCUnaryResponseHandler alloc] initWithResponseHandler:handler responseDispatchQueue:nil] callOptions:nil]; [call start]; call = [_service getFeatureWithMessage:[RTGPoint message] From 9ec763a80010188a107639423a2cee95d1dfb0d1 Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Wed, 27 Nov 2019 10:41:27 -0800 Subject: [PATCH 3/7] Add doc to notice that users should not reuse GRPCUnaryResponseHandler --- src/objective-c/ProtoRPC/ProtoRPC.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/objective-c/ProtoRPC/ProtoRPC.h b/src/objective-c/ProtoRPC/ProtoRPC.h index 7f0e68fbea0..f6dfbb14440 100644 --- a/src/objective-c/ProtoRPC/ProtoRPC.h +++ b/src/objective-c/ProtoRPC/ProtoRPC.h @@ -73,6 +73,9 @@ NS_ASSUME_NONNULL_BEGIN /** * A convenience class of objects that act as response handlers of calls. Issues * response to a single handler when the response is completed. + * + * The object is stateful and should not be reused for multiple calls. If multiple calls share the + * same response handling logic, create separate GRPCUnaryResponseHandler objects for each call. */ @interface GRPCUnaryResponseHandler : NSObject From f830dde7c81883534dda472729b5fe064038cfcf Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Wed, 27 Nov 2019 11:10:27 -0800 Subject: [PATCH 4/7] Remove GRPCProtoResponseHandler implementation since no longer needed --- .../objective-c/route_guide/ViewControllers.m | 29 +------------------ 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/examples/objective-c/route_guide/ViewControllers.m b/examples/objective-c/route_guide/ViewControllers.m index deddc05699d..55a193c11ae 100644 --- a/examples/objective-c/route_guide/ViewControllers.m +++ b/examples/objective-c/route_guide/ViewControllers.m @@ -68,7 +68,7 @@ static NSString * const kHostAddress = @"localhost:50051"; * Run the getFeature demo. Calls getFeature with a point known to have a feature and a point known * not to have a feature. */ -@interface GetFeatureViewController : UIViewController +@interface GetFeatureViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *outputLabel; @@ -78,33 +78,6 @@ static NSString * const kHostAddress = @"localhost:50051"; RTGRouteGuide *_service; } -- (dispatch_queue_t)dispatchQueue { - return dispatch_get_main_queue(); -} - -- (void)didReceiveProtoMessage:(GPBMessage *)message { - RTGFeature *response = (RTGFeature *)message; - - // TODO(makdharma): Remove boilerplate by consolidating into one log function. - if (response.name.length != 0) { - NSString *str =[NSString stringWithFormat:@"%@\nFound feature called %@ at %@.", self.outputLabel.text, response.location, response.name]; - self.outputLabel.text = str; - NSLog(@"Found feature called %@ at %@.", response.name, response.location); - } else if (response) { - NSString *str =[NSString stringWithFormat:@"%@\nFound no features at %@", self.outputLabel.text,response.location]; - self.outputLabel.text = str; - NSLog(@"Found no features at %@", response.location); - } -} - -- (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error { - if (error) { - NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; - self.outputLabel.text = str; - NSLog(@"RPC error: %@", error); - } -} - - (void)execRequest { void (^handler)(RTGFeature *response, NSError *error) = ^(RTGFeature *response, NSError *error) { // TODO(makdharma): Remove boilerplate by consolidating into one log function. From 264d71be4cefeac8132539783ce24b8ed3186c45 Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Wed, 27 Nov 2019 11:10:55 -0800 Subject: [PATCH 5/7] Update RecordRoute --- .../objective-c/route_guide/ViewControllers.m | 53 ++++++++----------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/examples/objective-c/route_guide/ViewControllers.m b/examples/objective-c/route_guide/ViewControllers.m index 55a193c11ae..290f7269fa2 100644 --- a/examples/objective-c/route_guide/ViewControllers.m +++ b/examples/objective-c/route_guide/ViewControllers.m @@ -206,7 +206,7 @@ static NSString * const kHostAddress = @"localhost:50051"; * database with a variable delay in between. Prints the statistics when they are sent from the * server. */ -@interface RecordRouteViewController : UIViewController +@interface RecordRouteViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *outputLabel; @@ -216,10 +216,6 @@ static NSString * const kHostAddress = @"localhost:50051"; RTGRouteGuide *_service; } -- (dispatch_queue_t)dispatchQueue { - return dispatch_get_main_queue(); -} - - (void)execRequest { NSString *dataBasePath = [NSBundle.mainBundle pathForResource:@"route_guide_db" ofType:@"json"]; @@ -234,7 +230,27 @@ static NSString * const kHostAddress = @"localhost:50051"; return; } - GRPCStreamingProtoCall *call = [_service recordRouteWithResponseHandler:self + void (^handler)(RTGRouteSummary *response, NSError *error) = ^(RTGRouteSummary *response, NSError *error) { + if (response) { + NSString *str =[NSString stringWithFormat: + @"%@\nFinished trip with %i points\nPassed %i features\n" + "Travelled %i meters\nIt took %i seconds", + self.outputLabel.text, response.pointCount, response.featureCount, + response.distance, response.elapsedTime]; + self.outputLabel.text = str; + NSLog(@"Finished trip with %i points", response.pointCount); + NSLog(@"Passed %i features", response.featureCount); + NSLog(@"Travelled %i meters", response.distance); + NSLog(@"It took %i seconds", response.elapsedTime); + } else { + NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; + self.outputLabel.text = str; + NSLog(@"RPC error: %@", error); + } + }; + + GRPCStreamingProtoCall *call = [_service recordRouteWithResponseHandler:[[GRPCUnaryResponseHandler alloc] initWithResponseHandler:handler + responseDispatchQueue:nil] callOptions:nil]; [call start]; for (id feature in features) { @@ -249,31 +265,6 @@ static NSString * const kHostAddress = @"localhost:50051"; [call finish]; } -- (void)didReceiveProtoMessage:(GPBMessage *)message { - RTGRouteSummary *response = (RTGRouteSummary *)message; - - if (response) { - NSString *str =[NSString stringWithFormat: - @"%@\nFinished trip with %i points\nPassed %i features\n" - "Travelled %i meters\nIt took %i seconds", - self.outputLabel.text, response.pointCount, response.featureCount, - response.distance, response.elapsedTime]; - self.outputLabel.text = str; - NSLog(@"Finished trip with %i points", response.pointCount); - NSLog(@"Passed %i features", response.featureCount); - NSLog(@"Travelled %i meters", response.distance); - NSLog(@"It took %i seconds", response.elapsedTime); - } -} - -- (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error { - if (error) { - NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; - self.outputLabel.text = str; - NSLog(@"RPC error: %@", error); - } -} - - (void)viewDidLoad { [super viewDidLoad]; From c0b85e5a469268981a80774ee86d00d4434511af Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Wed, 27 Nov 2019 11:31:09 -0800 Subject: [PATCH 6/7] clang-format --- .../objective-c/route_guide/ViewControllers.m | 143 ++++++++++-------- 1 file changed, 78 insertions(+), 65 deletions(-) diff --git a/examples/objective-c/route_guide/ViewControllers.m b/examples/objective-c/route_guide/ViewControllers.m index 290f7269fa2..28e801fd515 100644 --- a/examples/objective-c/route_guide/ViewControllers.m +++ b/examples/objective-c/route_guide/ViewControllers.m @@ -25,7 +25,7 @@ #import -static NSString * const kHostAddress = @"localhost:50051"; +static NSString *const kHostAddress = @"localhost:50051"; /** Category to override RTGPoint's description. */ @interface RTGPoint (Description) @@ -36,9 +36,9 @@ static NSString * const kHostAddress = @"localhost:50051"; - (NSString *)description { NSString *verticalDirection = self.latitude >= 0 ? @"N" : @"S"; NSString *horizontalDirection = self.longitude >= 0 ? @"E" : @"W"; - return [NSString stringWithFormat:@"%.02f%@ %.02f%@", - abs(self.latitude) / 1E7f, verticalDirection, - abs(self.longitude) / 1E7f, horizontalDirection]; + return + [NSString stringWithFormat:@"%.02f%@ %.02f%@", abs(self.latitude) / 1E7f, verticalDirection, + abs(self.longitude) / 1E7f, horizontalDirection]; } @end @@ -55,13 +55,12 @@ static NSString * const kHostAddress = @"localhost:50051"; longitude:(float)longitude { RTGRouteNote *note = [self message]; note.message = message; - note.location.latitude = (int32_t) latitude * 1E7; - note.location.longitude = (int32_t) longitude * 1E7; + note.location.latitude = (int32_t)latitude * 1E7; + note.location.longitude = (int32_t)longitude * 1E7; return note; } @end - #pragma mark Demo: Get Feature /** @@ -70,7 +69,7 @@ static NSString * const kHostAddress = @"localhost:50051"; */ @interface GetFeatureViewController : UIViewController -@property (weak, nonatomic) IBOutlet UILabel *outputLabel; +@property(weak, nonatomic) IBOutlet UILabel *outputLabel; @end @@ -82,15 +81,19 @@ static NSString * const kHostAddress = @"localhost:50051"; void (^handler)(RTGFeature *response, NSError *error) = ^(RTGFeature *response, NSError *error) { // TODO(makdharma): Remove boilerplate by consolidating into one log function. if (response.name.length) { - NSString *str =[NSString stringWithFormat:@"%@\nFound feature called %@ at %@.", self.outputLabel.text, response.location, response.name]; + NSString *str = + [NSString stringWithFormat:@"%@\nFound feature called %@ at %@.", self.outputLabel.text, + response.location, response.name]; self.outputLabel.text = str; NSLog(@"Found feature called %@ at %@.", response.name, response.location); } else if (response) { - NSString *str =[NSString stringWithFormat:@"%@\nFound no features at %@", self.outputLabel.text,response.location]; + NSString *str = [NSString stringWithFormat:@"%@\nFound no features at %@", + self.outputLabel.text, response.location]; self.outputLabel.text = str; NSLog(@"Found no features at %@", response.location); } else { - NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; + NSString *str = + [NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; self.outputLabel.text = str; NSLog(@"RPC error: %@", error); } @@ -100,15 +103,18 @@ static NSString * const kHostAddress = @"localhost:50051"; point.latitude = 409146138; point.longitude = -746188906; - GRPCUnaryProtoCall *call = [_service getFeatureWithMessage:point - responseHandler:[[GRPCUnaryResponseHandler alloc] initWithResponseHandler:handler responseDispatchQueue:nil] - callOptions:nil]; + GRPCUnaryProtoCall *call = [_service + getFeatureWithMessage:point + responseHandler:[[GRPCUnaryResponseHandler alloc] initWithResponseHandler:handler + responseDispatchQueue:nil] + callOptions:nil]; [call start]; - call = [_service getFeatureWithMessage:[RTGPoint message] - responseHandler:[[GRPCUnaryResponseHandler alloc] initWithResponseHandler:handler responseDispatchQueue:nil] - callOptions:nil]; + call = [_service + getFeatureWithMessage:[RTGPoint message] + responseHandler:[[GRPCUnaryResponseHandler alloc] initWithResponseHandler:handler + responseDispatchQueue:nil] + callOptions:nil]; [call start]; - } - (void)viewDidLoad { @@ -129,16 +135,15 @@ static NSString * const kHostAddress = @"localhost:50051"; @end - #pragma mark Demo: List Features /** * Run the listFeatures demo. Calls listFeatures with a rectangle containing all of the features in * the pre-generated database. Prints each response as it comes in. */ -@interface ListFeaturesViewController : UIViewController +@interface ListFeaturesViewController : UIViewController -@property (weak, nonatomic) IBOutlet UILabel *outputLabel; +@property(weak, nonatomic) IBOutlet UILabel *outputLabel; @end @@ -167,7 +172,9 @@ static NSString * const kHostAddress = @"localhost:50051"; - (void)didReceiveProtoMessage:(GPBMessage *)message { RTGFeature *response = (RTGFeature *)message; if (response) { - NSString *str =[NSString stringWithFormat:@"%@\nFound feature at %@ called %@.", self.outputLabel.text, response.location, response.name]; + NSString *str = + [NSString stringWithFormat:@"%@\nFound feature at %@ called %@.", self.outputLabel.text, + response.location, response.name]; self.outputLabel.text = str; NSLog(@"Found feature at %@ called %@.", response.location, response.name); } @@ -175,7 +182,7 @@ static NSString * const kHostAddress = @"localhost:50051"; - (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error { if (error) { - NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; + NSString *str = [NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; self.outputLabel.text = str; NSLog(@"RPC error: %@", error); } @@ -208,7 +215,7 @@ static NSString * const kHostAddress = @"localhost:50051"; */ @interface RecordRouteViewController : UIViewController -@property (weak, nonatomic) IBOutlet UILabel *outputLabel; +@property(weak, nonatomic) IBOutlet UILabel *outputLabel; @end @@ -217,11 +224,12 @@ static NSString * const kHostAddress = @"localhost:50051"; } - (void)execRequest { - NSString *dataBasePath = [NSBundle.mainBundle pathForResource:@"route_guide_db" - ofType:@"json"]; + NSString *dataBasePath = [NSBundle.mainBundle pathForResource:@"route_guide_db" ofType:@"json"]; NSData *dataBaseContent = [NSData dataWithContentsOfFile:dataBasePath]; NSError *error; - NSArray *features = [NSJSONSerialization JSONObjectWithData:dataBaseContent options:0 error:&error]; + NSArray *features = [NSJSONSerialization JSONObjectWithData:dataBaseContent + options:0 + error:&error]; if (error) { NSLog(@"Error reading database."); @@ -230,34 +238,39 @@ static NSString * const kHostAddress = @"localhost:50051"; return; } - void (^handler)(RTGRouteSummary *response, NSError *error) = ^(RTGRouteSummary *response, NSError *error) { - if (response) { - NSString *str =[NSString stringWithFormat: - @"%@\nFinished trip with %i points\nPassed %i features\n" - "Travelled %i meters\nIt took %i seconds", - self.outputLabel.text, response.pointCount, response.featureCount, - response.distance, response.elapsedTime]; - self.outputLabel.text = str; - NSLog(@"Finished trip with %i points", response.pointCount); - NSLog(@"Passed %i features", response.featureCount); - NSLog(@"Travelled %i meters", response.distance); - NSLog(@"It took %i seconds", response.elapsedTime); - } else { - NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; - self.outputLabel.text = str; - NSLog(@"RPC error: %@", error); - } - }; - - GRPCStreamingProtoCall *call = [_service recordRouteWithResponseHandler:[[GRPCUnaryResponseHandler alloc] initWithResponseHandler:handler - responseDispatchQueue:nil] - callOptions:nil]; + void (^handler)(RTGRouteSummary *response, NSError *error) = + ^(RTGRouteSummary *response, NSError *error) { + if (response) { + NSString *str = [NSString + stringWithFormat:@"%@\nFinished trip with %i points\nPassed %i features\n" + "Travelled %i meters\nIt took %i seconds", + self.outputLabel.text, response.pointCount, response.featureCount, + response.distance, response.elapsedTime]; + self.outputLabel.text = str; + NSLog(@"Finished trip with %i points", response.pointCount); + NSLog(@"Passed %i features", response.featureCount); + NSLog(@"Travelled %i meters", response.distance); + NSLog(@"It took %i seconds", response.elapsedTime); + } else { + NSString *str = + [NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; + self.outputLabel.text = str; + NSLog(@"RPC error: %@", error); + } + }; + + GRPCStreamingProtoCall *call = + [_service recordRouteWithResponseHandler:[[GRPCUnaryResponseHandler alloc] + initWithResponseHandler:handler + responseDispatchQueue:nil] + callOptions:nil]; [call start]; for (id feature in features) { RTGPoint *location = [RTGPoint message]; - location.longitude = [((NSNumber *) feature[@"location"][@"longitude"]) intValue]; - location.latitude = [((NSNumber *) feature[@"location"][@"latitude"]) intValue]; - NSString *str =[NSString stringWithFormat:@"%@\nVisiting point %@", self.outputLabel.text, location]; + location.longitude = [((NSNumber *)feature[@"location"][@"longitude"]) intValue]; + location.latitude = [((NSNumber *)feature[@"location"][@"latitude"]) intValue]; + NSString *str = + [NSString stringWithFormat:@"%@\nVisiting point %@", self.outputLabel.text, location]; self.outputLabel.text = str; NSLog(@"Visiting point %@", location); [call writeMessage:location]; @@ -283,16 +296,15 @@ static NSString * const kHostAddress = @"localhost:50051"; @end - #pragma mark Demo: Route Chat /** * Run the routeChat demo. Send some chat messages, and print any chat messages that are sent from * the server. */ -@interface RouteChatViewController : UIViewController +@interface RouteChatViewController : UIViewController -@property (weak, nonatomic) IBOutlet UILabel *outputLabel; +@property(weak, nonatomic) IBOutlet UILabel *outputLabel; @end @@ -305,13 +317,14 @@ static NSString * const kHostAddress = @"localhost:50051"; } - (void)execRequest { - NSArray *notes = @[[RTGRouteNote noteWithMessage:@"First message" latitude:0 longitude:0], - [RTGRouteNote noteWithMessage:@"Second message" latitude:0 longitude:1], - [RTGRouteNote noteWithMessage:@"Third message" latitude:1 longitude:0], - [RTGRouteNote noteWithMessage:@"Fourth message" latitude:0 longitude:0]]; - - GRPCStreamingProtoCall *call = [_service routeChatWithResponseHandler:self - callOptions:nil]; + NSArray *notes = @[ + [RTGRouteNote noteWithMessage:@"First message" latitude:0 longitude:0], + [RTGRouteNote noteWithMessage:@"Second message" latitude:0 longitude:1], + [RTGRouteNote noteWithMessage:@"Third message" latitude:1 longitude:0], + [RTGRouteNote noteWithMessage:@"Fourth message" latitude:0 longitude:0] + ]; + + GRPCStreamingProtoCall *call = [_service routeChatWithResponseHandler:self callOptions:nil]; [call start]; for (RTGRouteNote *note in notes) { [call writeMessage:note]; @@ -322,8 +335,8 @@ static NSString * const kHostAddress = @"localhost:50051"; - (void)didReceiveProtoMessage:(GPBMessage *)message { RTGRouteNote *note = (RTGRouteNote *)message; if (note) { - NSString *str =[NSString stringWithFormat:@"%@\nGot message %@ at %@", - self.outputLabel.text, note.message, note.location]; + NSString *str = [NSString stringWithFormat:@"%@\nGot message %@ at %@", self.outputLabel.text, + note.message, note.location]; self.outputLabel.text = str; NSLog(@"Got message %@ at %@", note.message, note.location); } @@ -333,7 +346,7 @@ static NSString * const kHostAddress = @"localhost:50051"; if (!error) { NSLog(@"Chat ended."); } else { - NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; + NSString *str = [NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; self.outputLabel.text = str; NSLog(@"RPC error: %@", error); } From 9bda9e9cb50e014f2b6f0d69df1f4ace50d5d824 Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Wed, 27 Nov 2019 13:36:41 -0800 Subject: [PATCH 7/7] Add additional comments --- examples/objective-c/route_guide/ViewControllers.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/objective-c/route_guide/ViewControllers.m b/examples/objective-c/route_guide/ViewControllers.m index 28e801fd515..17736bac84c 100644 --- a/examples/objective-c/route_guide/ViewControllers.m +++ b/examples/objective-c/route_guide/ViewControllers.m @@ -259,6 +259,8 @@ static NSString *const kHostAddress = @"localhost:50051"; } }; + // We can use unary response handler here because, despite the requests being a stream, the + // response of the RPC is unary. GRPCStreamingProtoCall *call = [_service recordRouteWithResponseHandler:[[GRPCUnaryResponseHandler alloc] initWithResponseHandler:handler