Update GRPCUnaryResponseHandler with generics

pull/21316/head
Muxi Yan 5 years ago
parent d0578ceb31
commit 38d361c398
  1. 19
      examples/objective-c/route_guide/ViewControllers.m
  2. 4
      src/objective-c/ProtoRPC/ProtoRPC.h
  3. 4
      src/objective-c/ProtoRPC/ProtoRPC.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];

@ -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<GRPCProtoResponseHandler>
@interface GRPCUnaryResponseHandler<ResponseType> : NSObject<GRPCProtoResponseHandler>
/**
* 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. */

@ -28,13 +28,13 @@
#import <RxLibrary/GRXWriter+Transformations.h>
@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;

Loading…
Cancel
Save