FIRDatabase.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #import <FirebaseAuthInterop/FIRAuthInterop.h>
  18. #import <FirebaseCore/FIRAppInternal.h>
  19. #import <FirebaseCore/FIRComponent.h>
  20. #import <FirebaseCore/FIRComponentContainer.h>
  21. #import <FirebaseCore/FIRDependency.h>
  22. #import <FirebaseCore/FIRLogger.h>
  23. #import <FirebaseCore/FIROptions.h>
  24. #import "FIRDatabase.h"
  25. #import "FIRDatabaseComponent.h"
  26. #import "FIRDatabaseConfig_Private.h"
  27. #import "FIRDatabaseQuery_Private.h"
  28. #import "FIRDatabaseReference_Private.h"
  29. #import "FIRDatabase_Private.h"
  30. #import "FRepoInfo.h"
  31. #import "FValidation.h"
  32. @implementation FIRDatabase
  33. // The STR and STR_EXPAND macro allow a numeric version passed to he compiler driver
  34. // with a -D to be treated as a string instead of an invalid floating point value.
  35. #define STR(x) STR_EXPAND(x)
  36. #define STR_EXPAND(x) #x
  37. static const char *FIREBASE_SEMVER = (const char *)STR(FIRDatabase_VERSION);
  38. + (FIRDatabase *)database {
  39. if (![FIRApp isDefaultAppConfigured]) {
  40. [NSException raise:@"FIRAppNotConfigured"
  41. format:@"Failed to get default Firebase Database instance. Must call `[FIRApp "
  42. @"configure]` (`FirebaseApp.configure()` in Swift) before using "
  43. @"Firebase Database."];
  44. }
  45. return [FIRDatabase databaseForApp:[FIRApp defaultApp]];
  46. }
  47. + (FIRDatabase *)databaseWithURL:(NSString *)url {
  48. FIRApp *app = [FIRApp defaultApp];
  49. if (app == nil) {
  50. [NSException raise:@"FIRAppNotConfigured"
  51. format:@"Failed to get default Firebase Database instance. "
  52. @"Must call `[FIRApp configure]` (`FirebaseApp.configure()` in "
  53. @"Swift) before using Firebase Database."];
  54. }
  55. return [FIRDatabase databaseForApp:app URL:url];
  56. }
  57. + (FIRDatabase *)databaseForApp:(FIRApp *)app {
  58. if (app == nil) {
  59. [NSException raise:@"InvalidFIRApp" format:@"nil FIRApp instance passed to databaseForApp."];
  60. }
  61. return [FIRDatabase databaseForApp:app URL:app.options.databaseURL];
  62. }
  63. + (FIRDatabase *)databaseForApp:(FIRApp *)app URL:(NSString *)url {
  64. if (app == nil) {
  65. [NSException raise:@"InvalidFIRApp"
  66. format:@"nil FIRApp instance passed to databaseForApp."];
  67. }
  68. if (url == nil) {
  69. [NSException raise:@"MissingDatabaseURL"
  70. format:@"Failed to get FirebaseDatabase instance: "
  71. @"Specify DatabaseURL within FIRApp or from your databaseForApp:URL: call."];
  72. }
  73. id<FIRDatabaseProvider> provider = FIR_COMPONENT(FIRDatabaseProvider, app.container);
  74. return [provider databaseForApp:app URL:url];
  75. }
  76. + (NSString *) buildVersion {
  77. // TODO: Restore git hash when build moves back to git
  78. return [NSString stringWithFormat:@"%s_%s", FIREBASE_SEMVER, __DATE__];
  79. }
  80. + (FIRDatabase *)createDatabaseForTests:(FRepoInfo *)repoInfo config:(FIRDatabaseConfig *)config {
  81. FIRDatabase *db = [[FIRDatabase alloc] initWithApp:nil repoInfo:repoInfo config:config];
  82. [db ensureRepo];
  83. return db;
  84. }
  85. + (NSString *) sdkVersion {
  86. return [NSString stringWithUTF8String:FIREBASE_SEMVER];
  87. }
  88. + (void) setLoggingEnabled:(BOOL)enabled {
  89. [FUtilities setLoggingEnabled:enabled];
  90. FFLog(@"I-RDB024001", @"BUILD Version: %@", [FIRDatabase buildVersion]);
  91. }
  92. - (id)initWithApp:(FIRApp *)app repoInfo:(FRepoInfo *)info config:(FIRDatabaseConfig *)config {
  93. self = [super init];
  94. if (self != nil) {
  95. self->_repoInfo = info;
  96. self->_config = config;
  97. self->_app = app;
  98. }
  99. return self;
  100. }
  101. - (FIRDatabaseReference *)reference {
  102. [self ensureRepo];
  103. return [[FIRDatabaseReference alloc] initWithRepo:self.repo path:[FPath empty]];
  104. }
  105. - (FIRDatabaseReference *)referenceWithPath:(NSString *)path {
  106. [self ensureRepo];
  107. [FValidation validateFrom:@"referenceWithPath" validRootPathString:path];
  108. FPath *childPath = [[FPath alloc] initWith:path];
  109. return [[FIRDatabaseReference alloc] initWithRepo:self.repo path:childPath];
  110. }
  111. - (FIRDatabaseReference *)referenceFromURL:(NSString *)databaseUrl {
  112. [self ensureRepo];
  113. if (databaseUrl == nil) {
  114. [NSException raise:@"InvalidDatabaseURL" format:@"Invalid nil url passed to referenceFromURL:"];
  115. }
  116. FParsedUrl *parsedUrl = [FUtilities parseUrl:databaseUrl];
  117. [FValidation validateFrom:@"referenceFromURL:" validURL:parsedUrl];
  118. if (![parsedUrl.repoInfo.host isEqualToString:_repoInfo.host]) {
  119. [NSException raise:@"InvalidDatabaseURL" format:@"Invalid URL (%@) passed to getReference(). URL was expected "
  120. "to match configured Database URL: %@", databaseUrl, [self reference].URL];
  121. }
  122. return [[FIRDatabaseReference alloc] initWithRepo:self.repo path:parsedUrl.path];
  123. }
  124. - (void)purgeOutstandingWrites {
  125. [self ensureRepo];
  126. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  127. [self.repo purgeOutstandingWrites];
  128. });
  129. }
  130. - (void)goOnline {
  131. [self ensureRepo];
  132. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  133. [self.repo resume];
  134. });
  135. }
  136. - (void)goOffline {
  137. [self ensureRepo];
  138. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  139. [self.repo interrupt];
  140. });
  141. }
  142. - (void)setPersistenceEnabled:(BOOL)persistenceEnabled {
  143. [self assertUnfrozen:@"setPersistenceEnabled"];
  144. self->_config.persistenceEnabled = persistenceEnabled;
  145. }
  146. - (BOOL)persistenceEnabled {
  147. return self->_config.persistenceEnabled;
  148. }
  149. - (void)setPersistenceCacheSizeBytes:(NSUInteger)persistenceCacheSizeBytes {
  150. [self assertUnfrozen:@"setPersistenceCacheSizeBytes"];
  151. self->_config.persistenceCacheSizeBytes = persistenceCacheSizeBytes;
  152. }
  153. - (NSUInteger)persistenceCacheSizeBytes {
  154. return self->_config.persistenceCacheSizeBytes;
  155. }
  156. - (void)setCallbackQueue:(dispatch_queue_t)callbackQueue {
  157. [self assertUnfrozen:@"setCallbackQueue"];
  158. self->_config.callbackQueue = callbackQueue;
  159. }
  160. - (dispatch_queue_t)callbackQueue {
  161. return self->_config.callbackQueue;
  162. }
  163. - (void) assertUnfrozen:(NSString*)methodName {
  164. if (self.repo != nil) {
  165. [NSException raise:@"FIRDatabaseAlreadyInUse" format:@"Calls to %@ must be made before any other usage of "
  166. "FIRDatabase instance.", methodName];
  167. }
  168. }
  169. - (void) ensureRepo {
  170. if (self.repo == nil) {
  171. self.repo = [FRepoManager createRepo:self.repoInfo config:self.config database:self];
  172. }
  173. }
  174. @end