Files
firebase-ios-sdk/FirebaseDatabase/Sources/Api/FIRDatabase.m
T

258 lines
8.7 KiB
Objective-C
Raw Normal View History

2017-05-15 12:27:07 -07:00
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <Foundation/Foundation.h>
#import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
#import "Interop/Auth/Public/FIRAuthInterop.h"
2018-11-19 09:52:42 -05:00
2020-06-22 07:58:52 -07:00
#import "FirebaseDatabase/Sources/Api/FIRDatabaseComponent.h"
#import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseQuery_Private.h"
#import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseReference_Private.h"
#import "FirebaseDatabase/Sources/Api/Private/FIRDatabase_Private.h"
#import "FirebaseDatabase/Sources/Core/FRepoInfo.h"
#import "FirebaseDatabase/Sources/FIRDatabaseConfig_Private.h"
2020-07-31 07:22:02 -07:00
#import "FirebaseDatabase/Sources/Public/FirebaseDatabase/FIRDatabase.h"
2020-06-22 07:58:52 -07:00
#import "FirebaseDatabase/Sources/Utilities/FValidation.h"
2017-05-15 12:27:07 -07:00
@implementation FIRDatabase
+ (FIRDatabase *)database {
if (![FIRApp isDefaultAppConfigured]) {
[NSException raise:@"FIRAppNotConfigured"
2019-08-05 13:27:47 -07:00
format:@"Failed to get default Firebase Database instance. "
@"Must call `[FIRApp "
@"configure]` (`FirebaseApp.configure()` in Swift) "
@"before using "
@"Firebase Database."];
2017-05-15 12:27:07 -07:00
}
2018-11-22 06:44:42 -05:00
return [FIRDatabase databaseForApp:[FIRApp defaultApp]];
2017-05-15 12:27:07 -07:00
}
+ (FIRDatabase *)databaseWithURL:(NSString *)url {
FIRApp *app = [FIRApp defaultApp];
if (app == nil) {
2019-08-05 13:27:47 -07:00
[NSException
raise:@"FIRAppNotConfigured"
format:
@"Failed to get default Firebase Database instance. "
@"Must call `[FIRApp configure]` (`FirebaseApp.configure()` in "
@"Swift) before using Firebase Database."];
}
return [FIRDatabase databaseForApp:app URL:url];
}
+ (FIRDatabase *)databaseForApp:(FIRApp *)app {
2017-05-15 12:27:07 -07:00
if (app == nil) {
2019-08-05 13:27:47 -07:00
[NSException raise:@"InvalidFIRApp"
format:@"nil FIRApp instance passed to databaseForApp."];
2017-05-15 12:27:07 -07:00
}
2020-08-26 20:04:41 +02:00
NSString *url = app.options.databaseURL;
if (!url) {
if (!app.options.projectID) {
[NSException
raise:@"MissingProjectId"
format:@"Can't determine Firebase Database URL. Be sure to "
@"include a Project ID when calling "
@"`FirebaseApp.configure()`."];
}
FFLog(@"I-RDB024002", @"Using default host for project %@",
app.options.projectID);
url = [NSString
stringWithFormat:@"https://%@-default-rtdb.firebaseio.com",
app.options.projectID];
}
return [FIRDatabase databaseForApp:app URL:url];
}
+ (FIRDatabase *)databaseForApp:(FIRApp *)app URL:(NSString *)url {
2019-08-05 13:27:47 -07:00
if (app == nil) {
[NSException raise:@"InvalidFIRApp"
format:@"nil FIRApp instance passed to databaseForApp."];
}
if (url == nil) {
[NSException raise:@"MissingDatabaseURL"
format:@"Failed to get FirebaseDatabase instance: "
@"Specify DatabaseURL within FIRApp or from your "
@"databaseForApp:URL: call."];
}
id<FIRDatabaseProvider> provider =
FIR_COMPONENT(FIRDatabaseProvider, app.container);
return [provider databaseForApp:app URL:url];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
+ (NSString *)buildVersion {
2017-05-15 12:27:07 -07:00
// TODO: Restore git hash when build moves back to git
2020-10-07 15:03:20 -07:00
return [NSString stringWithFormat:@"%@_%s", FIRFirebaseVersion(), __DATE__];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
+ (FIRDatabase *)createDatabaseForTests:(FRepoInfo *)repoInfo
config:(FIRDatabaseConfig *)config {
FIRDatabase *db = [[FIRDatabase alloc] initWithApp:nil
repoInfo:repoInfo
config:config];
2017-05-15 12:27:07 -07:00
[db ensureRepo];
return db;
}
2019-08-05 13:27:47 -07:00
+ (NSString *)sdkVersion {
2020-10-07 15:03:20 -07:00
return FIRFirebaseVersion();
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
+ (void)setLoggingEnabled:(BOOL)enabled {
2017-05-15 12:27:07 -07:00
[FUtilities setLoggingEnabled:enabled];
FFLog(@"I-RDB024001", @"BUILD Version: %@", [FIRDatabase buildVersion]);
}
2019-08-05 13:27:47 -07:00
- (id)initWithApp:(FIRApp *)app
repoInfo:(FRepoInfo *)info
config:(FIRDatabaseConfig *)config {
2017-05-15 12:27:07 -07:00
self = [super init];
if (self != nil) {
self->_repoInfo = info;
self->_config = config;
self->_app = app;
2017-05-15 12:27:07 -07:00
}
return self;
}
- (FIRDatabaseReference *)reference {
[self ensureRepo];
2019-08-05 13:27:47 -07:00
return [[FIRDatabaseReference alloc] initWithRepo:self.repo
path:[FPath empty]];
2017-05-15 12:27:07 -07:00
}
- (FIRDatabaseReference *)referenceWithPath:(NSString *)path {
[self ensureRepo];
[FValidation validateFrom:@"referenceWithPath" validRootPathString:path];
FPath *childPath = [[FPath alloc] initWith:path];
return [[FIRDatabaseReference alloc] initWithRepo:self.repo path:childPath];
}
- (FIRDatabaseReference *)referenceFromURL:(NSString *)databaseUrl {
[self ensureRepo];
if (databaseUrl == nil) {
2019-08-05 13:27:47 -07:00
[NSException raise:@"InvalidDatabaseURL"
format:@"Invalid nil url passed to referenceFromURL:"];
2017-05-15 12:27:07 -07:00
}
FParsedUrl *parsedUrl = [FUtilities parseUrl:databaseUrl];
[FValidation validateFrom:@"referenceFromURL:" validURL:parsedUrl];
BOOL isInvalidHost =
!parsedUrl.repoInfo.isCustomHost &&
![_repoInfo.host isEqualToString:parsedUrl.repoInfo.host];
if (isInvalidHost) {
[NSException raise:@"InvalidDatabaseURL"
format:@"Invalid URL (%@) passed to getReference(). URL "
@"was expected to match configured Database URL: %@",
databaseUrl, _repoInfo.host];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
return [[FIRDatabaseReference alloc] initWithRepo:self.repo
path:parsedUrl.path];
2017-05-15 12:27:07 -07:00
}
- (void)purgeOutstandingWrites {
[self ensureRepo];
dispatch_async([FIRDatabaseQuery sharedQueue], ^{
2019-08-05 13:27:47 -07:00
[self.repo purgeOutstandingWrites];
2017-05-15 12:27:07 -07:00
});
}
- (void)useEmulatorWithHost:(NSString *)host port:(NSInteger)port {
if (host.length == 0) {
[NSException raise:NSInvalidArgumentException
format:@"Cannot connect to nil or empty host."];
}
if (self.repo != nil) {
[NSException
raise:NSInternalInconsistencyException
format:@"Cannot connect to emulator after database initialization. "
@"Call useEmulator(host:port:) before creating a database "
@"reference or trying to load data."];
}
NSString *fullHost =
[NSString stringWithFormat:@"%@:%li", host, (long)port];
FRepoInfo *emulatorInfo = [[FRepoInfo alloc] initWithInfo:self.repoInfo
emulatedHost:fullHost];
self->_repoInfo = emulatorInfo;
}
2017-05-15 12:27:07 -07:00
- (void)goOnline {
[self ensureRepo];
dispatch_async([FIRDatabaseQuery sharedQueue], ^{
2019-08-05 13:27:47 -07:00
[self.repo resume];
2017-05-15 12:27:07 -07:00
});
}
- (void)goOffline {
[self ensureRepo];
dispatch_async([FIRDatabaseQuery sharedQueue], ^{
2019-08-05 13:27:47 -07:00
[self.repo interrupt];
2017-05-15 12:27:07 -07:00
});
}
- (void)setPersistenceEnabled:(BOOL)persistenceEnabled {
[self assertUnfrozen:@"setPersistenceEnabled"];
self->_config.persistenceEnabled = persistenceEnabled;
}
- (BOOL)persistenceEnabled {
return self->_config.persistenceEnabled;
}
- (void)setPersistenceCacheSizeBytes:(NSUInteger)persistenceCacheSizeBytes {
[self assertUnfrozen:@"setPersistenceCacheSizeBytes"];
self->_config.persistenceCacheSizeBytes = persistenceCacheSizeBytes;
}
- (NSUInteger)persistenceCacheSizeBytes {
return self->_config.persistenceCacheSizeBytes;
}
- (void)setCallbackQueue:(dispatch_queue_t)callbackQueue {
[self assertUnfrozen:@"setCallbackQueue"];
self->_config.callbackQueue = callbackQueue;
}
- (dispatch_queue_t)callbackQueue {
return self->_config.callbackQueue;
}
2019-08-05 13:27:47 -07:00
- (void)assertUnfrozen:(NSString *)methodName {
2017-05-15 12:27:07 -07:00
if (self.repo != nil) {
2019-08-05 13:27:47 -07:00
[NSException
raise:@"FIRDatabaseAlreadyInUse"
format:@"Calls to %@ must be made before any other usage of "
"FIRDatabase instance.",
methodName];
2017-05-15 12:27:07 -07:00
}
}
2019-08-05 13:27:47 -07:00
- (void)ensureRepo {
2017-05-15 12:27:07 -07:00
if (self.repo == nil) {
2019-08-05 13:27:47 -07:00
self.repo = [FRepoManager createRepo:self.repoInfo
config:self.config
database:self];
2017-05-15 12:27:07 -07:00
}
}
@end