diff options
author | Sebastian Schmidt <mrschmidt@google.com> | 2017-09-15 16:45:46 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-15 16:45:46 -0700 |
commit | c6bde890ce2b352f86aa699b28f829d4cd85424c (patch) | |
tree | 4bb647eacb640c75d8447d4d2edc83d304ec2adb /Firebase/Database/Core | |
parent | 06a7c4f330fd7cdc01d16a15a277df538a49b25d (diff) |
Adding Multi-Resource support to the Firebase iOS SDK (#278)
* Adding Multi-Resource support to the Firebase iOS SDK.
This CL also makes RepoInfo hashable and simplifies RepoManager based on this.
Diffstat (limited to 'Firebase/Database/Core')
-rw-r--r-- | Firebase/Database/Core/FRepoInfo.h | 6 | ||||
-rw-r--r-- | Firebase/Database/Core/FRepoInfo.m | 19 | ||||
-rw-r--r-- | Firebase/Database/Core/FRepoManager.m | 55 |
3 files changed, 53 insertions, 27 deletions
diff --git a/Firebase/Database/Core/FRepoInfo.h b/Firebase/Database/Core/FRepoInfo.h index dace937..433bf35 100644 --- a/Firebase/Database/Core/FRepoInfo.h +++ b/Firebase/Database/Core/FRepoInfo.h @@ -16,7 +16,7 @@ #import <Foundation/Foundation.h> -@interface FRepoInfo : NSObject +@interface FRepoInfo : NSObject <NSCopying> @property (nonatomic, readonly, strong) NSString* host; @property (nonatomic, readonly, strong) NSString* namespace; @@ -31,4 +31,8 @@ - (BOOL) isDemoHost; - (BOOL) isCustomHost; +- (id)copyWithZone:(NSZone *)zone; +- (NSUInteger)hash; +- (BOOL)isEqual:(id)anObject; + @end diff --git a/Firebase/Database/Core/FRepoInfo.m b/Firebase/Database/Core/FRepoInfo.m index 6b15fe5..925163e 100644 --- a/Firebase/Database/Core/FRepoInfo.m +++ b/Firebase/Database/Core/FRepoInfo.m @@ -112,4 +112,23 @@ return url; } +- (id)copyWithZone:(NSZone *)zone; { + return self; // Immutable +} + +- (NSUInteger)hash { + NSUInteger result = host.hash; + result = 31 * result + (secure ? 1 : 0); + result = 31 * result + namespace.hash; + result = 31 * result + host.hash; + return result; +} + +- (BOOL)isEqual:(id)anObject { + if (![anObject isKindOfClass:[FRepoInfo class]]) return NO; + FRepoInfo *other = (FRepoInfo *)anObject; + return secure == other.secure && [host isEqualToString:other.host] && + [namespace isEqualToString:other.namespace]; +} + @end diff --git a/Firebase/Database/Core/FRepoManager.m b/Firebase/Database/Core/FRepoManager.m index 6a134d2..c5194d5 100644 --- a/Firebase/Database/Core/FRepoManager.m +++ b/Firebase/Database/Core/FRepoManager.m @@ -24,9 +24,13 @@ @implementation FRepoManager -+ (NSMutableDictionary *)configs { +typedef NSMutableDictionary<NSString *, + NSMutableDictionary<FRepoInfo *, FRepo *> *> + FRepoDictionary; + ++ (FRepoDictionary *)configs { static dispatch_once_t pred = 0; - static NSMutableDictionary *configs; + static FRepoDictionary *configs; dispatch_once(&pred, ^{ configs = [NSMutableDictionary dictionary]; }); @@ -39,33 +43,32 @@ */ + (FRepo *) getRepo:(FRepoInfo *)repoInfo config:(FIRDatabaseConfig *)config { [config freeze]; - NSString* repoHashString = [NSString stringWithFormat:@"%@_%@", repoInfo.host, repoInfo.namespace]; - NSMutableDictionary *configs = [FRepoManager configs]; + FRepoDictionary *configs = [FRepoManager configs]; @synchronized(configs) { - NSMutableDictionary *repos = configs[config.sessionIdentifier]; - if (!repos || repos[repoHashString] == nil) { + NSMutableDictionary<FRepoInfo *, FRepo *> *repos = configs[config.sessionIdentifier]; + if (!repos || repos[repoInfo] == nil) { // Calling this should create the repo. [FIRDatabase createDatabaseForTests:repoInfo config:config]; } - return configs[config.sessionIdentifier][repoHashString]; + return configs[config.sessionIdentifier][repoInfo]; } } + (FRepo *) createRepo:(FRepoInfo *)repoInfo config:(FIRDatabaseConfig *)config database:(FIRDatabase *)database { [config freeze]; - NSString* repoHashString = [NSString stringWithFormat:@"%@_%@", repoInfo.host, repoInfo.namespace]; - NSMutableDictionary *configs = [FRepoManager configs]; + FRepoDictionary *configs = [FRepoManager configs]; @synchronized(configs) { - NSMutableDictionary *repos = configs[config.sessionIdentifier]; + NSMutableDictionary<FRepoInfo *, FRepo *> *repos = + configs[config.sessionIdentifier]; if (!repos) { repos = [NSMutableDictionary dictionary]; configs[config.sessionIdentifier] = repos; } - FRepo *repo = repos[repoHashString]; + FRepo *repo = repos[repoInfo]; if (repo == nil) { repo = [[FRepo alloc] initWithRepoInfo:repoInfo config:config database:database]; - repos[repoHashString] = repo; + repos[repoInfo] = repo; return repo; } else { [NSException raise:@"RepoExists" format:@"createRepo called for Repo that already exists."]; @@ -76,9 +79,9 @@ + (void) interrupt:(FIRDatabaseConfig *)config { dispatch_async([FIRDatabaseQuery sharedQueue], ^{ - NSMutableDictionary *configs = [FRepoManager configs]; - NSMutableDictionary *repos = configs[config.sessionIdentifier]; - for (FRepo* repo in [repos allValues]) { + FRepoDictionary *configs = [FRepoManager configs]; + NSMutableDictionary<FRepoInfo *, FRepo *> *repos = configs[config.sessionIdentifier]; + for (FRepo *repo in [repos allValues]) { [repo interrupt]; } }); @@ -86,20 +89,20 @@ + (void) interruptAll { dispatch_async([FIRDatabaseQuery sharedQueue], ^{ - NSMutableDictionary *configs = [FRepoManager configs]; - for (NSMutableDictionary *repos in [configs allValues]) { - for (FRepo* repo in [repos allValues]) { - [repo interrupt]; - } + FRepoDictionary *configs = [FRepoManager configs]; + for (NSMutableDictionary<FRepoInfo *, FRepo *> *repos in [configs allValues]) { + for (FRepo *repo in [repos allValues]) { + [repo interrupt]; + } } }); } + (void) resume:(FIRDatabaseConfig *)config { dispatch_async([FIRDatabaseQuery sharedQueue], ^{ - NSMutableDictionary *configs = [FRepoManager configs]; - NSMutableDictionary *repos = configs[config.sessionIdentifier]; - for (FRepo* repo in [repos allValues]) { + FRepoDictionary *configs = [FRepoManager configs]; + NSMutableDictionary<FRepoInfo *, FRepo *> *repos = configs[config.sessionIdentifier]; + for (FRepo *repo in [repos allValues]) { [repo resume]; } }); @@ -107,9 +110,9 @@ + (void) resumeAll { dispatch_async([FIRDatabaseQuery sharedQueue], ^{ - NSMutableDictionary *configs = [FRepoManager configs]; - for (NSMutableDictionary *repos in [configs allValues]) { - for (FRepo* repo in [repos allValues]) { + FRepoDictionary *configs = [FRepoManager configs]; + for (NSMutableDictionary<FRepoInfo *, FRepo *> *repos in [configs allValues]) { + for (FRepo *repo in [repos allValues]) { [repo resume]; } } |