fix(ios): potential race condition fix (#3484)

Co-authored-by: Mike Diarmid <mike.diarmid@gmail.com>
This commit is contained in:
Russell Wheatley
2020-06-22 13:04:24 +01:00
committed by GitHub
parent ad6bb41b93
commit ca34cef160
7 changed files with 48 additions and 21 deletions

View File

@@ -25,6 +25,8 @@
:(FIRApp *)firebaseApp
dbURL:(NSString *)dbURL;
+ (dispatch_queue_t)getDispatchQueue;
+ (void)setDatabaseConfig:(FIRDatabase *)firDatabase dbURL:(NSString *)dbURL;
+ (FIRDatabaseReference *)getReferenceForDatabase:(FIRDatabase *)firebaseDatabase path:(NSString *)path;

View File

@@ -33,8 +33,16 @@ NSString *const DATABASE_PERSISTENCE_CACHE_SIZE = @"firebase_database_persistenc
configSettingsLock = [NSMutableDictionary dictionary];
}
+ (FIRDatabase *)getDatabaseForApp:(FIRApp *)firebaseApp dbURL:(NSString *)dbURL {
+ (dispatch_queue_t)getDispatchQueue {
static dispatch_once_t once;
__strong static dispatch_queue_t sharedInstance;
dispatch_once(&once, ^{
sharedInstance = dispatch_queue_create("io.invertase.firebase.database", DISPATCH_QUEUE_SERIAL);
});
return sharedInstance;
}
+ (FIRDatabase *)getDatabaseForApp:(FIRApp *)firebaseApp dbURL:(NSString *)dbURL {
FIRDatabase *firDatabase;
if (dbURL == nil && dbURL.length == 0) {
@@ -56,13 +64,16 @@ NSString *const DATABASE_PERSISTENCE_CACHE_SIZE = @"firebase_database_persistenc
[lockKey appendString:dbURL];
}
if (configSettingsLock[lockKey]) {
return;
@synchronized (configSettingsLock) {
if (configSettingsLock[lockKey]) {
return;
}
}
RNFBPreferences *preferences = [RNFBPreferences shared];
@try {
firDatabase.callbackQueue = [RNFBDatabaseCommon getDispatchQueue];
// Persistence enabled
BOOL *persistenceEnabled = (BOOL *) [preferences getBooleanValue:DATABASE_PERSISTENCE_ENABLED defaultValue:false];
[firDatabase setPersistenceEnabled:(BOOL) persistenceEnabled];
@@ -76,9 +87,18 @@ NSString *const DATABASE_PERSISTENCE_CACHE_SIZE = @"firebase_database_persistenc
NSInteger *cacheSizeBytes = [preferences getIntegerValue:DATABASE_PERSISTENCE_CACHE_SIZE defaultValue:(NSInteger *) 10000000];
[firDatabase setPersistenceCacheSizeBytes:(NSUInteger) cacheSizeBytes];
}
@synchronized (configSettingsLock) {
configSettingsLock[lockKey] = @YES;
}
} @catch (NSException *exception) {
if (![@"FIRDatabaseAlreadyInUse" isEqualToString:exception.name]) {
@throw exception;
} else {
@synchronized (configSettingsLock) {
configSettingsLock[lockKey] = @YES;
}
}
}
}
@@ -86,23 +106,28 @@ NSString *const DATABASE_PERSISTENCE_CACHE_SIZE = @"firebase_database_persistenc
+ (FIRDatabaseReference *)getReferenceForDatabase
:(FIRDatabase *)firebaseDatabase
path:(NSString *)path {
return [firebaseDatabase referenceWithPath:path];
@synchronized (configSettingsLock) {
return [firebaseDatabase referenceWithPath:path];
}
}
+ (FIRDatabaseReference *)getReferenceForDatabase
:(NSString *)key
firebaseDatabase:(FIRDatabase *)firebaseDatabase
path:(NSString *)path {
FIRDatabaseReference *cachedReference = references[key];
@synchronized (configSettingsLock) {
FIRDatabaseReference *cachedReference = references[key];
if (cachedReference != nil) {
return cachedReference;
if (cachedReference != nil) {
return cachedReference;
}
FIRDatabaseReference *databaseReference = [firebaseDatabase referenceWithPath:path];
references[key] = databaseReference;
return databaseReference;
}
FIRDatabaseReference *databaseReference = [firebaseDatabase referenceWithPath:path];
references[key] = databaseReference;
return databaseReference;
}
+ (void)removeReferenceByKey:(NSString *)key {
@@ -114,8 +139,8 @@ NSString *const DATABASE_PERSISTENCE_CACHE_SIZE = @"firebase_database_persistenc
error:(NSError *)error {
NSArray *codeAndMessage = [self getCodeAndMessage:error];
[RNFBSharedUtils rejectPromiseWithUserInfo:reject userInfo:(NSMutableDictionary *) @{
@"code": (NSString *) codeAndMessage[0],
@"message": (NSString *) codeAndMessage[1],
@"code": (NSString *) codeAndMessage[0],
@"message": (NSString *) codeAndMessage[1],
}];
}

View File

@@ -29,9 +29,10 @@
RCT_EXPORT_MODULE();
- (dispatch_queue_t)methodQueue {
return dispatch_queue_create("io.invertase.firebase.database", DISPATCH_QUEUE_SERIAL);
return [RNFBDatabaseCommon getDispatchQueue];
}
#pragma mark -
#pragma mark Firebase Database

View File

@@ -21,7 +21,6 @@
#import "RNFBDatabaseCommon.h"
#import "RNFBDatabaseOnDisconnectModule.h"
@implementation RNFBDatabaseOnDisconnectModule
#pragma mark -
#pragma mark Module Setup
@@ -29,7 +28,7 @@
RCT_EXPORT_MODULE();
- (dispatch_queue_t)methodQueue {
return dispatch_queue_create("io.invertase.firebase.database", DISPATCH_QUEUE_SERIAL);
return [RNFBDatabaseCommon getDispatchQueue];
}
#pragma mark -

View File

@@ -32,7 +32,7 @@ static NSString *const RNFB_DATABASE_SYNC = @"database_sync_event";
RCT_EXPORT_MODULE();
- (dispatch_queue_t)methodQueue {
return dispatch_queue_create("io.invertase.firebase.database", DISPATCH_QUEUE_SERIAL);
return [RNFBDatabaseCommon getDispatchQueue];
}
- (id)init {

View File

@@ -29,7 +29,7 @@
RCT_EXPORT_MODULE();
- (dispatch_queue_t)methodQueue {
return dispatch_queue_create("io.invertase.firebase.database", DISPATCH_QUEUE_SERIAL);
return [RNFBDatabaseCommon getDispatchQueue];
}
#pragma mark -

View File

@@ -33,7 +33,7 @@ static NSString *const RNFB_DATABASE_TRANSACTION_EVENT = @"database_transaction_
RCT_EXPORT_MODULE();
- (dispatch_queue_t)methodQueue {
return dispatch_queue_create("io.invertase.firebase.database", DISPATCH_QUEUE_SERIAL);
return [RNFBDatabaseCommon getDispatchQueue];
}
- (id)init {