FIRDatabase.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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
  34. // driver with a -D to be treated as a string instead of an invalid floating
  35. // point value.
  36. #define STR(x) STR_EXPAND(x)
  37. #define STR_EXPAND(x) #x
  38. static const char *FIREBASE_SEMVER = (const char *)STR(FIRDatabase_VERSION);
  39. + (FIRDatabase *)database {
  40. if (![FIRApp isDefaultAppConfigured]) {
  41. [NSException raise:@"FIRAppNotConfigured"
  42. format:@"Failed to get default Firebase Database instance. "
  43. @"Must call `[FIRApp "
  44. @"configure]` (`FirebaseApp.configure()` in Swift) "
  45. @"before using "
  46. @"Firebase Database."];
  47. }
  48. return [FIRDatabase databaseForApp:[FIRApp defaultApp]];
  49. }
  50. + (FIRDatabase *)databaseWithURL:(NSString *)url {
  51. FIRApp *app = [FIRApp defaultApp];
  52. if (app == nil) {
  53. [NSException
  54. raise:@"FIRAppNotConfigured"
  55. format:
  56. @"Failed to get default Firebase Database instance. "
  57. @"Must call `[FIRApp configure]` (`FirebaseApp.configure()` in "
  58. @"Swift) before using Firebase Database."];
  59. }
  60. return [FIRDatabase databaseForApp:app URL:url];
  61. }
  62. + (FIRDatabase *)databaseForApp:(FIRApp *)app {
  63. if (app == nil) {
  64. [NSException raise:@"InvalidFIRApp"
  65. format:@"nil FIRApp instance passed to databaseForApp."];
  66. }
  67. return [FIRDatabase databaseForApp:app URL:app.options.databaseURL];
  68. }
  69. + (FIRDatabase *)databaseForApp:(FIRApp *)app URL:(NSString *)url {
  70. if (app == nil) {
  71. [NSException raise:@"InvalidFIRApp"
  72. format:@"nil FIRApp instance passed to databaseForApp."];
  73. }
  74. if (url == nil) {
  75. [NSException raise:@"MissingDatabaseURL"
  76. format:@"Failed to get FirebaseDatabase instance: "
  77. @"Specify DatabaseURL within FIRApp or from your "
  78. @"databaseForApp:URL: call."];
  79. }
  80. id<FIRDatabaseProvider> provider =
  81. FIR_COMPONENT(FIRDatabaseProvider, app.container);
  82. return [provider databaseForApp:app URL:url];
  83. }
  84. + (NSString *)buildVersion {
  85. // TODO: Restore git hash when build moves back to git
  86. return [NSString stringWithFormat:@"%s_%s", FIREBASE_SEMVER, __DATE__];
  87. }
  88. + (FIRDatabase *)createDatabaseForTests:(FRepoInfo *)repoInfo
  89. config:(FIRDatabaseConfig *)config {
  90. FIRDatabase *db = [[FIRDatabase alloc] initWithApp:nil
  91. repoInfo:repoInfo
  92. config:config];
  93. [db ensureRepo];
  94. return db;
  95. }
  96. + (NSString *)sdkVersion {
  97. return [NSString stringWithUTF8String:FIREBASE_SEMVER];
  98. }
  99. + (void)setLoggingEnabled:(BOOL)enabled {
  100. [FUtilities setLoggingEnabled:enabled];
  101. FFLog(@"I-RDB024001", @"BUILD Version: %@", [FIRDatabase buildVersion]);
  102. }
  103. - (id)initWithApp:(FIRApp *)app
  104. repoInfo:(FRepoInfo *)info
  105. config:(FIRDatabaseConfig *)config {
  106. self = [super init];
  107. if (self != nil) {
  108. self->_repoInfo = info;
  109. self->_config = config;
  110. self->_app = app;
  111. }
  112. return self;
  113. }
  114. - (FIRDatabaseReference *)reference {
  115. [self ensureRepo];
  116. return [[FIRDatabaseReference alloc] initWithRepo:self.repo
  117. path:[FPath empty]];
  118. }
  119. - (FIRDatabaseReference *)referenceWithPath:(NSString *)path {
  120. [self ensureRepo];
  121. [FValidation validateFrom:@"referenceWithPath" validRootPathString:path];
  122. FPath *childPath = [[FPath alloc] initWith:path];
  123. return [[FIRDatabaseReference alloc] initWithRepo:self.repo path:childPath];
  124. }
  125. - (FIRDatabaseReference *)referenceFromURL:(NSString *)databaseUrl {
  126. [self ensureRepo];
  127. if (databaseUrl == nil) {
  128. [NSException raise:@"InvalidDatabaseURL"
  129. format:@"Invalid nil url passed to referenceFromURL:"];
  130. }
  131. FParsedUrl *parsedUrl = [FUtilities parseUrl:databaseUrl];
  132. [FValidation validateFrom:@"referenceFromURL:" validURL:parsedUrl];
  133. if (![parsedUrl.repoInfo.host isEqualToString:_repoInfo.host]) {
  134. [NSException
  135. raise:@"InvalidDatabaseURL"
  136. format:
  137. @"Invalid URL (%@) passed to getReference(). URL was expected "
  138. "to match configured Database URL: %@",
  139. databaseUrl, [self reference].URL];
  140. }
  141. return [[FIRDatabaseReference alloc] initWithRepo:self.repo
  142. path:parsedUrl.path];
  143. }
  144. - (void)purgeOutstandingWrites {
  145. [self ensureRepo];
  146. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  147. [self.repo purgeOutstandingWrites];
  148. });
  149. }
  150. - (void)goOnline {
  151. [self ensureRepo];
  152. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  153. [self.repo resume];
  154. });
  155. }
  156. - (void)goOffline {
  157. [self ensureRepo];
  158. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  159. [self.repo interrupt];
  160. });
  161. }
  162. - (void)setPersistenceEnabled:(BOOL)persistenceEnabled {
  163. [self assertUnfrozen:@"setPersistenceEnabled"];
  164. self->_config.persistenceEnabled = persistenceEnabled;
  165. }
  166. - (BOOL)persistenceEnabled {
  167. return self->_config.persistenceEnabled;
  168. }
  169. - (void)setPersistenceCacheSizeBytes:(NSUInteger)persistenceCacheSizeBytes {
  170. [self assertUnfrozen:@"setPersistenceCacheSizeBytes"];
  171. self->_config.persistenceCacheSizeBytes = persistenceCacheSizeBytes;
  172. }
  173. - (NSUInteger)persistenceCacheSizeBytes {
  174. return self->_config.persistenceCacheSizeBytes;
  175. }
  176. - (void)setCallbackQueue:(dispatch_queue_t)callbackQueue {
  177. [self assertUnfrozen:@"setCallbackQueue"];
  178. self->_config.callbackQueue = callbackQueue;
  179. }
  180. - (dispatch_queue_t)callbackQueue {
  181. return self->_config.callbackQueue;
  182. }
  183. - (void)assertUnfrozen:(NSString *)methodName {
  184. if (self.repo != nil) {
  185. [NSException
  186. raise:@"FIRDatabaseAlreadyInUse"
  187. format:@"Calls to %@ must be made before any other usage of "
  188. "FIRDatabase instance.",
  189. methodName];
  190. }
  191. }
  192. - (void)ensureRepo {
  193. if (self.repo == nil) {
  194. self.repo = [FRepoManager createRepo:self.repoInfo
  195. config:self.config
  196. database:self];
  197. }
  198. }
  199. @end