diff options
author | 2015-05-14 12:53:07 -0700 | |
---|---|---|
committer | 2015-05-14 12:53:07 -0700 | |
commit | dda9a3cd369726ebf88568f38c85bd4fc9db9094 (patch) | |
tree | 321eb4ffb911a9ac9798e55a2672075a4e3cd2ce | |
parent | ec4e5cf212219432b7512a39186dd73bcebe7e51 (diff) |
Made cache thread-safe
-rw-r--r-- | src/objective-c/GRPCClient/private/GRPCChannel.m | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/src/objective-c/GRPCClient/private/GRPCChannel.m b/src/objective-c/GRPCClient/private/GRPCChannel.m index e1aebdfe57..36f4c0aa5e 100644 --- a/src/objective-c/GRPCClient/private/GRPCChannel.m +++ b/src/objective-c/GRPCClient/private/GRPCChannel.m @@ -40,15 +40,15 @@ @implementation GRPCChannel -// TODO(mlumish): Investigate whether a cache with strong links is a good idea -static NSMutableDictionary *channelCache; - + (instancetype)channelToHost:(NSString *)host { - if (channelCache == nil) { + // TODO(mlumish): Investigate whether a cache with strong links is a good idea + static NSMutableDictionary *channelCache; + static dispatch_once_t cacheInitialization; + dispatch_once(&cacheInitialization, ^{ channelCache = [NSMutableDictionary dictionary]; - } + }); GRPCChannel *channel = channelCache[host]; - if (channel == nil) { + if (!channel) { channel = [[self alloc] initWithHost:host]; channelCache[host] = channel; } |