Skip to content

Commit

Permalink
fix(ios): potential race condition fix (#3484)
Browse files Browse the repository at this point in the history
Co-authored-by: Mike Diarmid <mike.diarmid@gmail.com>
  • Loading branch information
russellwheatley and Salakar authored Jun 22, 2020
1 parent ad6bb41 commit ca34cef
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 20 deletions.
2 changes: 2 additions & 0 deletions packages/database/ios/RNFBDatabase/RNFBDatabaseCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
53 changes: 39 additions & 14 deletions packages/database/ios/RNFBDatabase/RNFBDatabaseCommon.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ + (void)load {
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) {
Expand All @@ -56,13 +64,16 @@ + (void)setDatabaseConfig:(FIRDatabase *)firDatabase
[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];
Expand All @@ -76,33 +87,47 @@ + (void)setDatabaseConfig:(FIRDatabase *)firDatabase
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;
}
}
}
}

+ (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];
FIRDatabaseReference *databaseReference = [firebaseDatabase referenceWithPath:path];

references[key] = databaseReference;
return databaseReference;
references[key] = databaseReference;

return databaseReference;
}
}

+ (void)removeReferenceByKey:(NSString *)key {
Expand All @@ -114,8 +139,8 @@ + (void)promiseRejectDatabaseException
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],
}];
}

Expand Down
3 changes: 2 additions & 1 deletion packages/database/ios/RNFBDatabase/RNFBDatabaseModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ @implementation RNFBDatabaseModule
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@
#import "RNFBDatabaseCommon.h"
#import "RNFBDatabaseOnDisconnectModule.h"


@implementation RNFBDatabaseOnDisconnectModule
#pragma mark -
#pragma mark Module Setup

RCT_EXPORT_MODULE();

- (dispatch_queue_t)methodQueue {
return dispatch_queue_create("io.invertase.firebase.database", DISPATCH_QUEUE_SERIAL);
return [RNFBDatabaseCommon getDispatchQueue];
}

#pragma mark -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ @implementation RNFBDatabaseQueryModule
RCT_EXPORT_MODULE();

- (dispatch_queue_t)methodQueue {
return dispatch_queue_create("io.invertase.firebase.database", DISPATCH_QUEUE_SERIAL);
return [RNFBDatabaseCommon getDispatchQueue];
}

- (id)init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ @implementation RNFBDatabaseReferenceModule
RCT_EXPORT_MODULE();

- (dispatch_queue_t)methodQueue {
return dispatch_queue_create("io.invertase.firebase.database", DISPATCH_QUEUE_SERIAL);
return [RNFBDatabaseCommon getDispatchQueue];
}

#pragma mark -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ @implementation RNFBDatabaseTransactionModule
RCT_EXPORT_MODULE();

- (dispatch_queue_t)methodQueue {
return dispatch_queue_create("io.invertase.firebase.database", DISPATCH_QUEUE_SERIAL);
return [RNFBDatabaseCommon getDispatchQueue];
}

- (id)init {
Expand Down

0 comments on commit ca34cef

Please sign in to comment.