Skip to content

Commit 5fcfc44

Browse files
committed
fix(database, useEmulator): drop multiple calls to useEmulator
guards against case where hot-reload of javascript means state is lost and second native calls are attempted (and rejected...) Fixes #5650
1 parent 74af5ab commit 5fcfc44

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

packages/database/android/src/main/java/io/invertase/firebase/database/UniversalFirebaseDatabaseCommon.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ static FirebaseDatabase getDatabaseForApp(String appName, String dbURL) {
4646
setDatabaseConfig(firebaseDatabase, appName, dbURL);
4747

4848
HashMap emulatorConfig = getEmulatorConfig(appName, dbURL);
49-
if (emulatorConfig != null) {
49+
50+
if (emulatorConfig != null && emulatorConfig.get("configured") == null) {
5051
firebaseDatabase.useEmulator(
5152
(String) emulatorConfig.get("host"), (Integer) emulatorConfig.get("port"));
53+
// The underlying SDK may only be configured once, but with hot-reloads in the
54+
// javascript bundle, javascript cannot hold SDK configuration state. Keep track here
55+
// so we only configure once
56+
emulatorConfig.put("configured", "true");
5257
}
5358

5459
return firebaseDatabase;

packages/database/ios/RNFBDatabase/RNFBDatabaseModule.m

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#import "RNFBDatabaseModule.h"
2323
#import "RNFBPreferences.h"
2424

25+
static __strong NSMutableDictionary *emulatorSettings;
26+
2527
@implementation RNFBDatabaseModule
2628
#pragma mark -
2729
#pragma mark Module Setup
@@ -58,8 +60,22 @@ - (dispatch_queue_t)methodQueue {
5860
: (NSString *)dbURL
5961
: (nonnull NSString *)host
6062
: (NSInteger)port) {
61-
[[RNFBDatabaseCommon getDatabaseForApp:firebaseApp dbURL:dbURL] useEmulatorWithHost:host
62-
port:port];
63+
// javascript may hot reload, losing state, and native throws an error if you double-request
64+
// so we keep track of useEmulator calls here to avoid calling native twice
65+
if (emulatorSettings == nil) {
66+
emulatorSettings = [NSMutableDictionary dictionary];
67+
}
68+
69+
NSMutableString *configKey = [firebaseApp.name mutableCopy];
70+
if (dbURL != nil && dbURL.length > 0) {
71+
[configKey appendString:dbURL];
72+
}
73+
74+
if (!emulatorSettings[configKey]) {
75+
[[RNFBDatabaseCommon getDatabaseForApp:firebaseApp dbURL:dbURL] useEmulatorWithHost:host
76+
port:port];
77+
emulatorSettings[configKey] = @YES;
78+
}
6379
}
6480

6581
RCT_EXPORT_METHOD(setPersistenceEnabled

0 commit comments

Comments
 (0)