Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(firestore,ios): Settings incorrectly set multiple times #2869

Merged
merged 2 commits into from
Nov 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 34 additions & 40 deletions packages/firestore/ios/RNFBFirestore/RNFBFirestoreCommon.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
/**
* Copyright (c) 2016-present Invertase Limited & Contributors
*
Expand All @@ -25,15 +24,13 @@
NSString *const FIRESTORE_PERSISTENCE = @"firebase_firestore_persistence";
NSString *const FIRESTORE_SSL = @"firebase_firestore_ssl";

static __strong NSMutableDictionary *settingsLock;
__strong NSMutableDictionary *settingsLock;

@implementation RNFBFirestoreCommon

+ (FIRFirestore *)getFirestoreForApp
:(FIRApp *)app {
+ (FIRFirestore *)getFirestoreForApp:(FIRApp *)app {
FIRFirestore *instance = [FIRFirestore firestoreForApp:app];
[self setFirestoreSettings:instance appName:[RNFBSharedUtils getAppJavaScriptName:app.name]];

return instance;
}

Expand All @@ -47,62 +44,59 @@ + (dispatch_queue_t)getFirestoreQueue {
}

+ (void)setFirestoreSettings:(FIRFirestore *)firestore appName:(NSString *)appName {
// Prevent setting if already set
if (settingsLock[appName]) {
return;
}
@synchronized(settingsLock) {
if (settingsLock == nil) {
settingsLock = [[NSMutableDictionary alloc] init];
}

FIRFirestoreSettings *firestoreSettings = [[FIRFirestoreSettings alloc] init];
RNFBPreferences *preferences = [RNFBPreferences shared];
// Prevent setting if already set
if (settingsLock[appName]) {
return;
}

firestoreSettings.dispatchQueue = [self getFirestoreQueue];
FIRFirestoreSettings *firestoreSettings = [[FIRFirestoreSettings alloc] init];
RNFBPreferences *preferences = [RNFBPreferences shared];

NSString *cacheKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_CACHE_SIZE, appName];
NSInteger *size = [preferences getIntegerValue:cacheKey defaultValue:0];
firestoreSettings.dispatchQueue = [self getFirestoreQueue];

if (size == (NSInteger *) -1) {
firestoreSettings.cacheSizeBytes = kFIRFirestoreCacheSizeUnlimited;
} else if (size == 0) {
firestoreSettings.cacheSizeBytes = firestore.settings.cacheSizeBytes;
} else {
firestoreSettings.cacheSizeBytes = (int64_t) size;
}
NSString *cacheKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_CACHE_SIZE, appName];
NSInteger *size = [preferences getIntegerValue:cacheKey defaultValue:0];

NSString *hostKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_HOST, appName];
firestoreSettings.host = [preferences getStringValue:hostKey defaultValue:firestore.settings.host];
if (size == (NSInteger *) -1) {
firestoreSettings.cacheSizeBytes = kFIRFirestoreCacheSizeUnlimited;
} else if (size == 0) {
firestoreSettings.cacheSizeBytes = firestore.settings.cacheSizeBytes;
} else {
firestoreSettings.cacheSizeBytes = (int64_t) size;
}

NSString *persistenceKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_PERSISTENCE, appName];
firestoreSettings.persistenceEnabled = (BOOL) [preferences getBooleanValue:persistenceKey defaultValue:firestore.settings.persistenceEnabled];
NSString *hostKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_HOST, appName];
firestoreSettings.host = [preferences getStringValue:hostKey defaultValue:firestore.settings.host];

NSString *sslKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_SSL, appName];
firestoreSettings.sslEnabled = (BOOL) [preferences getBooleanValue:sslKey defaultValue:firestore.settings.sslEnabled];
NSString *persistenceKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_PERSISTENCE, appName];
firestoreSettings.persistenceEnabled = (BOOL) [preferences getBooleanValue:persistenceKey defaultValue:firestore.settings.persistenceEnabled];

settingsLock[appName] = @(YES);
firestore.settings = firestoreSettings;
NSString *sslKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_SSL, appName];
firestoreSettings.sslEnabled = (BOOL) [preferences getBooleanValue:sslKey defaultValue:firestore.settings.sslEnabled];

return;
settingsLock[appName] = @(YES);
firestore.settings = firestoreSettings;
}
}

+ (FIRDocumentReference *)getDocumentForFirestore
:(FIRFirestore *)firestore
path:(NSString *)path {
+ (FIRDocumentReference *)getDocumentForFirestore:(FIRFirestore *)firestore:(NSString *)path {
return [firestore documentWithPath:path];
}

+ (FIRQuery *)getQueryForFirestore
:(FIRFirestore *)firestore
path:(NSString *)path
type:(NSString *)type {
+ (FIRQuery *)getQueryForFirestore:(FIRFirestore *)firestore path:(NSString *)path type:(NSString *)type {
if ([type isEqualToString:@"collectionGroup"]) {
return [firestore collectionGroupWithID:path];
}

return [firestore collectionWithPath:path];
}

+ (void)promiseRejectFirestoreException
:(RCTPromiseRejectBlock)reject
error:(NSError *)error {
+ (void)promiseRejectFirestoreException:(RCTPromiseRejectBlock)reject error:(NSError *)error {
NSArray *codeAndMessage = [self getCodeAndMessage:error];
[RNFBSharedUtils rejectPromiseWithUserInfo:reject userInfo:(NSMutableDictionary *) @{
@"code": (NSString *) codeAndMessage[0],
Expand Down