Remove retain of handler in callbacks and dispatch to dispatchQueue

pull/16190/head
Muxi Yan 6 years ago
parent e39c146f0f
commit f3e9224f0b
  1. 11
      src/objective-c/GRPCClient/GRPCCall.h
  2. 21
      src/objective-c/GRPCClient/GRPCCall.m
  3. 8
      src/objective-c/ProtoRPC/ProtoRPC.h
  4. 63
      src/objective-c/ProtoRPC/ProtoRPC.m
  5. 24
      src/objective-c/tests/GRPCClientTests.m
  6. 24
      src/objective-c/tests/InteropTests.m

@ -155,12 +155,16 @@ extern NSString *const kGRPCTrailersKey;
@optional
/** Issued when initial metadata is received from the server. */
/**
* Issued when initial metadata is received from the server. The task must be scheduled onto the
* dispatch queue in property \a dispatchQueue.
*/
- (void)receivedInitialMetadata:(NSDictionary *_Nullable)initialMetadata;
/**
* Issued when a message is received from the server. The message is the raw data received from the
* server, with decompression and without proto deserialization.
* server, with decompression and without proto deserialization. The task must be scheduled onto the
* dispatch queue in property \a dispatchQueue.
*/
- (void)receivedRawMessage:(NSData *_Nullable)message;
@ -168,7 +172,8 @@ extern NSString *const kGRPCTrailersKey;
* Issued when a call finished. If the call finished successfully, \a error is nil and \a
* trainingMetadata consists any trailing metadata received from the server. Otherwise, \a error
* is non-nil and contains the corresponding error information, including gRPC error codes and
* error descriptions.
* error descriptions. The task must be scheduled onto the dispatch queue in property
* \a dispatchQueue.
*/
- (void)closedWithTrailingMetadata:(NSDictionary *_Nullable)trailingMetadata
error:(NSError *_Nullable)error;

@ -252,30 +252,21 @@ const char *kCFStreamVarName = "grpc_cfstream";
}
- (void)issueInitialMetadata:(NSDictionary *)initialMetadata {
id<GRPCResponseHandler> handler = _handler;
if ([handler respondsToSelector:@selector(receivedInitialMetadata:)]) {
dispatch_async(handler.dispatchQueue, ^{
[handler receivedInitialMetadata:initialMetadata];
});
if (initialMetadata != nil && [_handler respondsToSelector:@selector(receivedInitialMetadata:)]) {
[_handler receivedInitialMetadata:initialMetadata];
}
}
- (void)issueMessage:(id)message {
id<GRPCResponseHandler> handler = _handler;
if ([handler respondsToSelector:@selector(receivedRawMessage:)]) {
dispatch_async(handler.dispatchQueue, ^{
[handler receivedRawMessage:message];
});
if (message != nil && [_handler respondsToSelector:@selector(receivedRawMessage:)]) {
[_handler receivedRawMessage:message];
}
}
- (void)issueClosedWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
id<GRPCResponseHandler> handler = _handler;
NSDictionary *trailers = _call.responseTrailers;
if ([handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) {
dispatch_async(handler.dispatchQueue, ^{
[handler closedWithTrailingMetadata:trailers error:error];
});
if ([_handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) {
[_handler closedWithTrailingMetadata:trailers error:error];
}
}

@ -30,11 +30,14 @@ NS_ASSUME_NONNULL_BEGIN
@optional
/** Issued when initial metadata is received from the server. */
/**
* Issued when initial metadata is received from the server. The task must be scheduled onto the
* dispatch queue in property \a dispatchQueue. */
- (void)receivedInitialMetadata:(NSDictionary *_Nullable)initialMetadata;
/**
* Issued when a message is received from the server. The message is the deserialized proto object.
* The task must be scheduled onto the dispatch queue in property \a dispatchQueue.
*/
- (void)receivedProtoMessage:(GPBMessage *_Nullable)message;
@ -42,7 +45,8 @@ NS_ASSUME_NONNULL_BEGIN
* Issued when a call finished. If the call finished successfully, \a error is nil and \a
* trainingMetadata consists any trailing metadata received from the server. Otherwise, \a error
* is non-nil and contains the corresponding error information, including gRPC error codes and
* error descriptions.
* error descriptions. The task must be scheduled onto the dispatch queue in property
* \a dispatchQueue.
*/
- (void)closedWithTrailingMetadata:(NSDictionary *_Nullable)trailingMetadata
error:(NSError *_Nullable)error;

@ -172,53 +172,44 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
}
- (void)receivedInitialMetadata:(NSDictionary *_Nullable)initialMetadata {
if (_handler && initialMetadata != nil) {
id<GRPCProtoResponseHandler> handler = _handler;
if ([handler respondsToSelector:@selector(initialMetadata:)]) {
dispatch_async(handler.dispatchQueue, ^{
[handler receivedInitialMetadata:initialMetadata];
});
dispatch_async(_dispatchQueue, ^{
if (initialMetadata != nil && [self->_handler respondsToSelector:@selector(initialMetadata:)]) {
[self->_handler receivedInitialMetadata:initialMetadata];
}
}
});
}
- (void)receivedRawMessage:(NSData *_Nullable)message {
if (_handler && message != nil) {
id<GRPCProtoResponseHandler> handler = _handler;
NSError *error = nil;
GPBMessage *parsed = [_responseClass parseFromData:message error:&error];
if (parsed) {
if ([handler respondsToSelector:@selector(receivedProtoMessage:)]) {
dispatch_async(handler.dispatchQueue, ^{
[handler receivedProtoMessage:parsed];
});
}
} else {
if ([handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) {
dispatch_async(handler.dispatchQueue, ^{
[handler closedWithTrailingMetadata:nil error:ErrorForBadProto(message, _responseClass, error)];
});
dispatch_async(_dispatchQueue, ^{
if (self->_handler && message != nil) {
NSError *error = nil;
GPBMessage *parsed = [self->_responseClass parseFromData:message error:&error];
if (parsed) {
if ([self->_handler respondsToSelector:@selector(receivedProtoMessage:)]) {
[self->_handler receivedProtoMessage:parsed];
}
} else {
if ([self->_handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) {
[self->_handler closedWithTrailingMetadata:nil error:ErrorForBadProto(message, _responseClass, error)];
}
self->_handler = nil;
[self->_call cancel];
self->_call = nil;
}
_handler = nil;
[_call cancel];
_call = nil;
}
}
});
}
- (void)closedWithTrailingMetadata:(NSDictionary *_Nullable)trailingMetadata
error:(NSError *_Nullable)error {
if (_handler) {
id<GRPCProtoResponseHandler> handler = _handler;
if ([handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) {
dispatch_async(handler.dispatchQueue, ^{
[handler closedWithTrailingMetadata:trailingMetadata error:error];
});
dispatch_async(_dispatchQueue, ^{
if ([self->_handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) {
[self->_handler closedWithTrailingMetadata:trailingMetadata error:error];
}
_handler = nil;
}
[_call cancel];
_call = nil;
self->_handler = nil;
[self->_call cancel];
self->_call = nil;
});
}
- (dispatch_queue_t)dispatchQueue {

@ -115,22 +115,28 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
}
- (void)receivedInitialMetadata:(NSDictionary *_Nullable)initialMetadata {
if (_initialMetadataCallback) {
_initialMetadataCallback(initialMetadata);
}
dispatch_async(_dispatchQueue, ^{
if (_initialMetadataCallback) {
_initialMetadataCallback(initialMetadata);
}
});
}
- (void)receivedRawMessage:(GPBMessage *_Nullable)message {
if (_messageCallback) {
_messageCallback(message);
}
dispatch_async(_dispatchQueue, ^{
if (_messageCallback) {
_messageCallback(message);
}
});
}
- (void)closedWithTrailingMetadata:(NSDictionary *_Nullable)trailingMetadata
error:(NSError *_Nullable)error {
if (_closeCallback) {
_closeCallback(trailingMetadata, error);
}
dispatch_async(_dispatchQueue, ^{
if (_closeCallback) {
_closeCallback(trailingMetadata, error);
}
});
}
- (dispatch_queue_t)dispatchQueue {

@ -103,22 +103,28 @@ BOOL isRemoteInteropTest(NSString *host) {
}
- (void)receivedInitialMetadata:(NSDictionary *_Nullable)initialMetadata {
if (_initialMetadataCallback) {
_initialMetadataCallback(initialMetadata);
}
dispatch_async(_dispatchQueue, ^{
if (_initialMetadataCallback) {
_initialMetadataCallback(initialMetadata);
}
});
}
- (void)receivedProtoMessage:(GPBMessage *_Nullable)message {
if (_messageCallback) {
_messageCallback(message);
}
dispatch_async(_dispatchQueue, ^{
if (_messageCallback) {
_messageCallback(message);
}
});
}
- (void)closedWithTrailingMetadata:(NSDictionary *_Nullable)trailingMetadata
error:(NSError *_Nullable)error {
if (_closeCallback) {
_closeCallback(trailingMetadata, error);
}
dispatch_async(_dispatchQueue, ^{
if (_closeCallback) {
_closeCallback(trailingMetadata, error);
}
});
}
- (dispatch_queue_t)dispatchQueue {

Loading…
Cancel
Save