diff options
author | Muxi Yan <mxyan@google.com> | 2017-07-17 17:39:54 -0700 |
---|---|---|
committer | Muxi Yan <mxyan@google.com> | 2017-07-17 17:39:54 -0700 |
commit | 23f19d88545d1f56b7dbd73a887af6c6e61b7d2a (patch) | |
tree | 475ba493498671ab9e28abbb4126133324c12835 | |
parent | 8ad304ba16a6eb8e94b6d07dfc18c07b90a10e26 (diff) |
Move closure to a method of GRPCCall
-rw-r--r-- | src/objective-c/GRPCClient/GRPCCall.m | 68 |
1 files changed, 33 insertions, 35 deletions
diff --git a/src/objective-c/GRPCClient/GRPCCall.m b/src/objective-c/GRPCClient/GRPCCall.m index 0a1db7ea68..0f58bc40c8 100644 --- a/src/objective-c/GRPCClient/GRPCCall.m +++ b/src/objective-c/GRPCClient/GRPCCall.m @@ -415,6 +415,36 @@ static NSString * const kBearerPrefix = @"Bearer "; #pragma mark GRXWriter implementation +- (void)startCallWithWriteable:(id<GRXWriteable>)writeable { + _responseWriteable = [[GRXConcurrentWriteable alloc] initWithWriteable:writeable + dispatchQueue:_responseQueue]; + + _wrappedCall = [[GRPCWrappedCall alloc] initWithHost:_host + serverName:_serverName + path:_path]; + NSAssert(_wrappedCall, @"Error allocating RPC objects. Low memory?"); + + [self sendHeaders:_requestHeaders]; + [self invokeCall]; + + // TODO(jcanizales): Extract this logic somewhere common. + NSString *host = [NSURL URLWithString:[@"https://" stringByAppendingString:_host]].host; + if (!host) { + // TODO(jcanizales): Check this on init. + [NSException raise:NSInvalidArgumentException format:@"host of %@ is nil", _host]; + } + _connectivityMonitor = [GRPCConnectivityMonitor monitorWithHost:host]; + __weak typeof(self) weakSelf = self; + void (^handler)() = ^{ + typeof(self) strongSelf = weakSelf; + [strongSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain + code:GRPCErrorCodeUnavailable + userInfo:@{ NSLocalizedDescriptionKey : @"Connectivity lost." }]]; + }; + [_connectivityMonitor handleLossWithHandler:handler + wifiStatusChangeHandler:nil]; +} + - (void)startWithWriteable:(id<GRXWriteable>)writeable { @synchronized(self) { _state = GRXWriterStateStarted; @@ -427,41 +457,9 @@ static NSString * const kBearerPrefix = @"Bearer "; // that the life of the instance is determined by this retain cycle. _retainSelf = self; - __weak typeof(self) weakSelf = self; - void (^performCall)() = ^{ - typeof(self) strongSelf = weakSelf; - if (strongSelf) { - strongSelf->_responseWriteable = [[GRXConcurrentWriteable alloc] initWithWriteable:writeable - dispatchQueue:strongSelf->_responseQueue]; - - strongSelf->_wrappedCall = [[GRPCWrappedCall alloc] initWithHost:strongSelf->_host - serverName:strongSelf->_serverName - path:strongSelf->_path]; - NSAssert(_wrappedCall, @"Error allocating RPC objects. Low memory?"); - - [strongSelf sendHeaders:_requestHeaders]; - [strongSelf invokeCall]; - - // TODO(jcanizales): Extract this logic somewhere common. - NSString *host = [NSURL URLWithString:[@"https://" stringByAppendingString:strongSelf->_host]].host; - if (!host) { - // TODO(jcanizales): Check this on init. - [NSException raise:NSInvalidArgumentException format:@"host of %@ is nil", strongSelf->_host]; - } - strongSelf->_connectivityMonitor = [GRPCConnectivityMonitor monitorWithHost:host]; - void (^handler)() = ^{ - typeof(self) strongSelf = weakSelf; - [strongSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain - code:GRPCErrorCodeUnavailable - userInfo:@{ NSLocalizedDescriptionKey : @"Connectivity lost." }]]; - }; - [_connectivityMonitor handleLossWithHandler:handler - wifiStatusChangeHandler:nil]; - } - }; - if (self.oauthToken != nil) { self.isWaitingForToken = YES; + __weak typeof(self) weakSelf = self; [self.oauthToken getTokenWithHandler:^(NSString *token){ typeof(self) strongSelf = weakSelf; if (strongSelf && strongSelf.isWaitingForToken) { @@ -469,12 +467,12 @@ static NSString * const kBearerPrefix = @"Bearer "; NSString *t = [kBearerPrefix stringByAppendingString:token]; strongSelf.requestHeaders[kAuthorizationHeader] = t; } - performCall(); + [strongSelf startCallWithWriteable:writeable]; strongSelf.isWaitingForToken = NO; } }]; } else { - performCall(); + [self startCallWithWriteable:writeable]; } } |