FIRDatabase.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "FirebaseAuth/Interop/FIRAuthInterop.h"
  18. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  19. #import "FirebaseDatabase/Sources/Api/FIRDatabaseComponent.h"
  20. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseQuery_Private.h"
  21. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseReference_Private.h"
  22. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabase_Private.h"
  23. #import "FirebaseDatabase/Sources/Core/FRepoInfo.h"
  24. #import "FirebaseDatabase/Sources/FIRDatabaseConfig_Private.h"
  25. #import "FirebaseDatabase/Sources/Public/FirebaseDatabase/FIRDatabase.h"
  26. #import "FirebaseDatabase/Sources/Utilities/FValidation.h"
  27. @implementation FIRDatabase
  28. + (FIRDatabase *)database {
  29. if (![FIRApp isDefaultAppConfigured]) {
  30. [NSException
  31. raise:@"FIRAppNotConfigured"
  32. format:@"The default FirebaseApp instance must be "
  33. @"configured before the default Database instance "
  34. @"can be initialized. One way to ensure this is to "
  35. @"call `FirebaseApp.configure()` in the App Delegate's "
  36. @"`application(_:didFinishLaunchingWithOptions:)` "
  37. @"(or the `@main` struct's initializer in SwiftUI)."];
  38. }
  39. return [FIRDatabase databaseForApp:[FIRApp defaultApp]];
  40. }
  41. + (FIRDatabase *)databaseWithURL:(NSString *)url {
  42. FIRApp *app = [FIRApp defaultApp];
  43. if (app == nil) {
  44. [NSException
  45. raise:@"FIRAppNotConfigured"
  46. format:
  47. @"Failed to get default Firebase Database instance. "
  48. @"Must call `[FIRApp configure]` (`FirebaseApp.configure()` in "
  49. @"Swift) before using Firebase Database."];
  50. }
  51. return [FIRDatabase databaseForApp:app URL:url];
  52. }
  53. + (FIRDatabase *)databaseForApp:(FIRApp *)app {
  54. if (app == nil) {
  55. [NSException raise:@"InvalidFIRApp"
  56. format:@"nil FIRApp instance passed to databaseForApp."];
  57. }
  58. NSString *url = app.options.databaseURL;
  59. if (!url) {
  60. if (!app.options.projectID) {
  61. [NSException
  62. raise:@"MissingProjectId"
  63. format:@"Can't determine Firebase Database URL. Be sure to "
  64. @"include a Project ID when calling "
  65. @"`FirebaseApp.configure()`."];
  66. }
  67. FFLog(@"I-RDB024002", @"Using default host for project %@",
  68. app.options.projectID);
  69. url = [NSString
  70. stringWithFormat:@"https://%@-default-rtdb.firebaseio.com",
  71. app.options.projectID];
  72. }
  73. return [FIRDatabase databaseForApp:app URL:url];
  74. }
  75. + (FIRDatabase *)databaseForApp:(FIRApp *)app URL:(NSString *)url {
  76. if (app == nil) {
  77. [NSException raise:@"InvalidFIRApp"
  78. format:@"nil FIRApp instance passed to databaseForApp."];
  79. }
  80. if (url == nil) {
  81. [NSException raise:@"MissingDatabaseURL"
  82. format:@"Failed to get FirebaseDatabase instance: "
  83. @"Specify DatabaseURL within FIRApp or from your "
  84. @"databaseForApp:URL: call."];
  85. }
  86. id<FIRDatabaseProvider> provider =
  87. FIR_COMPONENT(FIRDatabaseProvider, app.container);
  88. return [provider databaseForApp:app URL:url];
  89. }
  90. + (NSString *)buildVersion {
  91. return [NSString stringWithFormat:@"%@_%s", FIRFirebaseVersion(), __DATE__];
  92. }
  93. + (FIRDatabase *)createDatabaseForTests:(FRepoInfo *)repoInfo
  94. config:(FIRDatabaseConfig *)config {
  95. FIRDatabase *db = [[FIRDatabase alloc] initWithApp:nil
  96. repoInfo:repoInfo
  97. config:config];
  98. [db ensureRepo];
  99. return db;
  100. }
  101. + (NSString *)sdkVersion {
  102. return FIRFirebaseVersion();
  103. }
  104. + (void)setLoggingEnabled:(BOOL)enabled {
  105. [FUtilities setLoggingEnabled:enabled];
  106. FFLog(@"I-RDB024001", @"BUILD Version: %@", [FIRDatabase buildVersion]);
  107. }
  108. - (id)initWithApp:(FIRApp *)app
  109. repoInfo:(FRepoInfo *)info
  110. config:(FIRDatabaseConfig *)config {
  111. self = [super init];
  112. if (self != nil) {
  113. self->_repoInfo = info;
  114. self->_config = config;
  115. self->_app = app;
  116. }
  117. return self;
  118. }
  119. - (FIRDatabaseReference *)reference {
  120. [self ensureRepo];
  121. return [[FIRDatabaseReference alloc] initWithRepo:self.repo
  122. path:[FPath empty]];
  123. }
  124. - (FIRDatabaseReference *)referenceWithPath:(NSString *)path {
  125. [self ensureRepo];
  126. [FValidation validateFrom:@"referenceWithPath" validRootPathString:path];
  127. FPath *childPath = [[FPath alloc] initWith:path];
  128. return [[FIRDatabaseReference alloc] initWithRepo:self.repo path:childPath];
  129. }
  130. - (FIRDatabaseReference *)referenceFromURL:(NSString *)databaseUrl {
  131. [self ensureRepo];
  132. if (databaseUrl == nil) {
  133. [NSException raise:@"InvalidDatabaseURL"
  134. format:@"Invalid nil url passed to referenceFromURL:"];
  135. }
  136. FParsedUrl *parsedUrl = [FUtilities parseUrl:databaseUrl];
  137. [FValidation validateFrom:@"referenceFromURL:" validURL:parsedUrl];
  138. BOOL isInvalidHost =
  139. !parsedUrl.repoInfo.isCustomHost &&
  140. ![_repoInfo.host isEqualToString:parsedUrl.repoInfo.host];
  141. if (isInvalidHost) {
  142. [NSException raise:@"InvalidDatabaseURL"
  143. format:@"Invalid URL (%@) passed to getReference(). URL "
  144. @"was expected to match configured Database URL: %@",
  145. databaseUrl, _repoInfo.host];
  146. }
  147. return [[FIRDatabaseReference alloc] initWithRepo:self.repo
  148. path:parsedUrl.path];
  149. }
  150. - (void)purgeOutstandingWrites {
  151. [self ensureRepo];
  152. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  153. [self.repo purgeOutstandingWrites];
  154. });
  155. }
  156. - (void)useEmulatorWithHost:(NSString *)host port:(NSInteger)port {
  157. if (host.length == 0) {
  158. [NSException raise:NSInvalidArgumentException
  159. format:@"Cannot connect to nil or empty host."];
  160. }
  161. if (self.repo != nil) {
  162. [NSException
  163. raise:NSInternalInconsistencyException
  164. format:@"Cannot connect to emulator after database initialization. "
  165. @"Call useEmulator(host:port:) before creating a database "
  166. @"reference or trying to load data."];
  167. }
  168. NSString *fullHost =
  169. [NSString stringWithFormat:@"%@:%li", host, (long)port];
  170. FRepoInfo *emulatorInfo = [[FRepoInfo alloc] initWithInfo:self.repoInfo
  171. emulatedHost:fullHost];
  172. self->_repoInfo = emulatorInfo;
  173. }
  174. - (void)goOnline {
  175. [self ensureRepo];
  176. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  177. [self.repo resume];
  178. });
  179. }
  180. - (void)goOffline {
  181. [self ensureRepo];
  182. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  183. [self.repo interrupt];
  184. });
  185. }
  186. - (void)setPersistenceEnabled:(BOOL)persistenceEnabled {
  187. [self assertUnfrozen:@"setPersistenceEnabled"];
  188. self->_config.persistenceEnabled = persistenceEnabled;
  189. }
  190. - (BOOL)persistenceEnabled {
  191. return self->_config.persistenceEnabled;
  192. }
  193. - (void)setPersistenceCacheSizeBytes:(NSUInteger)persistenceCacheSizeBytes {
  194. [self assertUnfrozen:@"setPersistenceCacheSizeBytes"];
  195. self->_config.persistenceCacheSizeBytes = persistenceCacheSizeBytes;
  196. }
  197. - (NSUInteger)persistenceCacheSizeBytes {
  198. return self->_config.persistenceCacheSizeBytes;
  199. }
  200. - (void)setCallbackQueue:(dispatch_queue_t)callbackQueue {
  201. [self assertUnfrozen:@"setCallbackQueue"];
  202. self->_config.callbackQueue = callbackQueue;
  203. }
  204. - (dispatch_queue_t)callbackQueue {
  205. return self->_config.callbackQueue;
  206. }
  207. - (void)assertUnfrozen:(NSString *)methodName {
  208. if (self.repo != nil) {
  209. [NSException
  210. raise:@"FIRDatabaseAlreadyInUse"
  211. format:@"Calls to %@ must be made before any other usage of "
  212. "FIRDatabase instance.",
  213. methodName];
  214. }
  215. }
  216. - (void)ensureRepo {
  217. if (self.repo == nil) {
  218. self.repo = [FRepoManager createRepo:self.repoInfo
  219. config:self.config
  220. database:self];
  221. }
  222. }
  223. @end