FIRDatabase.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. if (![parsedUrl.repoInfo.host isEqualToString:_repoInfo.host]) {
  138. [NSException
  139. raise:@"InvalidDatabaseURL"
  140. format:
  141. @"Invalid URL (%@) passed to getReference(). URL was expected "
  142. "to match configured Database URL: %@",
  143. databaseUrl, [self reference].URL];
  144. }
  145. return [[FIRDatabaseReference alloc] initWithRepo:self.repo
  146. path:parsedUrl.path];
  147. }
  148. - (void)purgeOutstandingWrites {
  149. [self ensureRepo];
  150. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  151. [self.repo purgeOutstandingWrites];
  152. });
  153. }
  154. - (void)goOnline {
  155. [self ensureRepo];
  156. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  157. [self.repo resume];
  158. });
  159. }
  160. - (void)goOffline {
  161. [self ensureRepo];
  162. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  163. [self.repo interrupt];
  164. });
  165. }
  166. - (void)setPersistenceEnabled:(BOOL)persistenceEnabled {
  167. [self assertUnfrozen:@"setPersistenceEnabled"];
  168. self->_config.persistenceEnabled = persistenceEnabled;
  169. }
  170. - (BOOL)persistenceEnabled {
  171. return self->_config.persistenceEnabled;
  172. }
  173. - (void)setPersistenceCacheSizeBytes:(NSUInteger)persistenceCacheSizeBytes {
  174. [self assertUnfrozen:@"setPersistenceCacheSizeBytes"];
  175. self->_config.persistenceCacheSizeBytes = persistenceCacheSizeBytes;
  176. }
  177. - (NSUInteger)persistenceCacheSizeBytes {
  178. return self->_config.persistenceCacheSizeBytes;
  179. }
  180. - (void)setCallbackQueue:(dispatch_queue_t)callbackQueue {
  181. [self assertUnfrozen:@"setCallbackQueue"];
  182. self->_config.callbackQueue = callbackQueue;
  183. }
  184. - (dispatch_queue_t)callbackQueue {
  185. return self->_config.callbackQueue;
  186. }
  187. - (void)assertUnfrozen:(NSString *)methodName {
  188. if (self.repo != nil) {
  189. [NSException
  190. raise:@"FIRDatabaseAlreadyInUse"
  191. format:@"Calls to %@ must be made before any other usage of "
  192. "FIRDatabase instance.",
  193. methodName];
  194. }
  195. }
  196. - (void)ensureRepo {
  197. if (self.repo == nil) {
  198. self.repo = [FRepoManager createRepo:self.repoInfo
  199. config:self.config
  200. database:self];
  201. }
  202. }
  203. @end