From 5743c80660b4a7adbbe0eb47af73677dc1b779ea Mon Sep 17 00:00:00 2001 From: russellwheatley Date: Fri, 15 Nov 2024 12:03:41 +0000 Subject: [PATCH] fix(firestore, android): synchronize access to firestore instances --- .../FlutterFirebaseFirestorePlugin.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/cloud_firestore/cloud_firestore/android/src/main/java/io/flutter/plugins/firebase/firestore/FlutterFirebaseFirestorePlugin.java b/packages/cloud_firestore/cloud_firestore/android/src/main/java/io/flutter/plugins/firebase/firestore/FlutterFirebaseFirestorePlugin.java index 35e4697f7128..a25b40ea4447 100644 --- a/packages/cloud_firestore/cloud_firestore/android/src/main/java/io/flutter/plugins/firebase/firestore/FlutterFirebaseFirestorePlugin.java +++ b/packages/cloud_firestore/cloud_firestore/android/src/main/java/io/flutter/plugins/firebase/firestore/FlutterFirebaseFirestorePlugin.java @@ -108,11 +108,13 @@ protected static void setCachedFirebaseFirestoreInstanceForKey( protected static FirebaseFirestore getFirestoreInstanceByNameAndDatabaseUrl( String appName, String databaseURL) { - for (Map.Entry entry : + synchronized (firestoreInstanceCache) { + for (Map.Entry entry : firestoreInstanceCache.entrySet()) { - if (entry.getValue().getInstance().getApp().getName().equals(appName) + if (entry.getValue().getInstance().getApp().getName().equals(appName) && entry.getValue().getDatabaseURL().equals(databaseURL)) { - return entry.getKey(); + return entry.getKey(); + } } } return null; @@ -200,12 +202,14 @@ public Task didReinitializeFirebaseCore() { () -> { try { // Context is ignored by API so we don't send it over even though annotated non-null. - for (Map.Entry entry : + synchronized (firestoreInstanceCache) { + for (Map.Entry entry : firestoreInstanceCache.entrySet()) { - FirebaseFirestore firestore = entry.getKey(); - Tasks.await(firestore.terminate()); - FlutterFirebaseFirestorePlugin.destroyCachedFirebaseFirestoreInstanceForKey( + FirebaseFirestore firestore = entry.getKey(); + Tasks.await(firestore.terminate()); + FlutterFirebaseFirestorePlugin.destroyCachedFirebaseFirestoreInstanceForKey( firestore); + } } removeEventListeners();