diff options
author | Muxi Yan <muxi@users.noreply.github.com> | 2018-02-28 16:04:29 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-28 16:04:29 -0800 |
commit | c6c5ce923ca5233566dbba2550dc2876c5c2c4ef (patch) | |
tree | ac28cbf77b59f422bea9f4de8585887aff654cc3 /src/objective-c/GRPCClient/private/GRPCHost.m | |
parent | baf1a412624d20d1c11f60ddd66d47e58b3e3f35 (diff) |
Revert "Refactor connectivity monitor on iOS"
Diffstat (limited to 'src/objective-c/GRPCClient/private/GRPCHost.m')
-rw-r--r-- | src/objective-c/GRPCClient/private/GRPCHost.m | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/src/objective-c/GRPCClient/private/GRPCHost.m b/src/objective-c/GRPCClient/private/GRPCHost.m index 8568e334dd..71b57cf1f6 100644 --- a/src/objective-c/GRPCClient/private/GRPCHost.m +++ b/src/objective-c/GRPCClient/private/GRPCHost.m @@ -37,6 +37,12 @@ NS_ASSUME_NONNULL_BEGIN static NSMutableDictionary *kHostCache; +// This connectivity monitor flushes the host cache when connectivity status +// changes or when connection switch between Wifi and Cellular data, so that a +// new call will use a new channel. Otherwise, a new call will still use the +// cached channel which is no longer available and will cause gRPC to hang. +static GRPCConnectivityMonitor *connectivityMonitor = nil; + @implementation GRPCHost { // TODO(mlumish): Investigate whether caching channels with strong links is a good idea. GRPCChannel *_channel; @@ -84,7 +90,17 @@ static NSMutableDictionary *kHostCache; kHostCache[address] = self; _compressAlgorithm = GRPC_COMPRESS_NONE; } - [GRPCConnectivityMonitor registerObserver:self selector:@selector(connectivityChange:)]; + // Keep a single monitor to flush the cache if the connectivity status changes + // Thread safety guarded by @synchronized(kHostCache) + if (!connectivityMonitor) { + connectivityMonitor = + [GRPCConnectivityMonitor monitorWithHost:hostURL.host]; + void (^handler)(void) = ^{ + [GRPCHost flushChannelCache]; + }; + [connectivityMonitor handleLossWithHandler:handler + wifiStatusChangeHandler:handler]; + } } return self; } @@ -265,13 +281,6 @@ static NSMutableDictionary *kHostCache; } } -// Flushes the host cache when connectivity status changes or when connection switch between Wifi -// and Cellular data, so that a new call will use a new channel. Otherwise, a new call will still -// use the cached channel which is no longer available and will cause gRPC to hang. -- (void)connectivityChange:(NSNotification *)note { - [GRPCHost flushChannelCache]; -} - @end NS_ASSUME_NONNULL_END |