Skip to content

Commit bad4528

Browse files
authored
fix(database, iOS): prevent duplicate database instance initialization in FLTFirebaseDatabasePlugin (#18373)
1 parent 214afc3 commit bad4528

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

packages/firebase_database/firebase_database/ios/firebase_database/Sources/firebase_database/FLTFirebaseDatabasePlugin.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ public class FLTFirebaseDatabasePlugin: NSObject, FlutterPlugin, FLTFirebasePlug
169169
app: DatabasePigeonFirebaseApp, host: String, port: Int64,
170170
completion: @escaping (Result<Void, Error>) -> Void
171171
) {
172+
let instanceKey = databaseInstanceKey(app)
173+
guard Self.cachedDatabaseInstances[instanceKey] == nil else {
174+
completion(.success(()))
175+
return
176+
}
177+
172178
let database = getDatabaseFromPigeonApp(app)
173179
database.useEmulator(withHost: host, port: Int(port))
174180
completion(.success(()))
@@ -733,8 +739,12 @@ public class FLTFirebaseDatabasePlugin: NSObject, FlutterPlugin, FLTFirebasePlug
733739

734740
// MARK: - Helper Methods
735741

742+
private func databaseInstanceKey(_ app: DatabasePigeonFirebaseApp) -> String {
743+
app.appName + (app.databaseURL ?? "")
744+
}
745+
736746
private func getDatabaseFromPigeonApp(_ app: DatabasePigeonFirebaseApp) -> Database {
737-
let instanceKey = app.appName + (app.databaseURL ?? "")
747+
let instanceKey = databaseInstanceKey(app)
738748

739749
if let cachedInstance = Self.cachedDatabaseInstances[instanceKey] {
740750
return cachedInstance

0 commit comments

Comments
 (0)