diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java b/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java index 81546875fcb..c9c3676f414 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java @@ -67,6 +67,9 @@ public class FirebaseInstallations implements FirebaseInstallationsApi { private final Object lock = new Object(); private final ExecutorService backgroundExecutor; private final ExecutorService networkExecutor; + /* FID of this Firebase Installations instance. Cached after successfully registering and + persisting the FID locally. NOTE: cachedFid resets if FID is deleted.*/ + private String cachedFid = null; @GuardedBy("lock") private final List listeners = new ArrayList<>(); @@ -284,6 +287,9 @@ private void triggerOnException(PersistedInstallationEntry prefs, Exception exce } private String doGetId() { + if (cachedFid != null) { + return cachedFid; + } PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe(); // Execute network calls (CreateInstallations) to the FIS Servers on a separate executor // i.e networkExecutor @@ -337,6 +343,11 @@ private void doNetworkCall(boolean forceRefresh) { // Store the prefs to persist the result of the previous step. insertOrUpdatePrefs(prefs); + // Update cachedFID, if FID is successfully REGISTERED and persisted. + if (prefs.isRegistered()) { + cachedFid = prefs.getFirebaseInstallationId(); + } + // Let the caller know about the result. if (prefs.isErrored()) { triggerOnException(prefs, new FirebaseInstallationsException(Status.BAD_CONFIG)); @@ -493,6 +504,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer( case AUTH_ERROR: // The the server refused to generate a new auth token due to bad credentials, clear the // FID to force the generation of a new one. + cachedFid = null; return prefs.withNoGeneratedFid(); default: throw new IOException(); @@ -504,6 +516,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer( * storage. */ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsException, IOException { + cachedFid = null; PersistedInstallationEntry entry = getMultiProcessSafePrefs(); if (entry.isRegistered()) { // Call the FIS servers to delete this Firebase Installation Id. @@ -519,7 +532,6 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio "Failed to delete a Firebase Installation.", Status.BAD_CONFIG); } } - insertOrUpdatePrefs(entry.withNoGeneratedFid()); return null; }