FIRDatabase.m 8.7 KB

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