@@ -67,6 +67,9 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
67
67
private final Object lock = new Object ();
68
68
private final ExecutorService backgroundExecutor ;
69
69
private final ExecutorService networkExecutor ;
70
+ /* FID of this Firebase Installations instance. Cached after successfully registering and
71
+ persisting the FID locally. NOTE: cachedFid resets if FID is deleted.*/
72
+ private String cachedFid = null ;
70
73
71
74
@ GuardedBy ("lock" )
72
75
private final List <StateListener > listeners = new ArrayList <>();
@@ -284,6 +287,9 @@ private void triggerOnException(PersistedInstallationEntry prefs, Exception exce
284
287
}
285
288
286
289
private String doGetId () {
290
+ if (cachedFid != null ) {
291
+ return cachedFid ;
292
+ }
287
293
PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe ();
288
294
// Execute network calls (CreateInstallations) to the FIS Servers on a separate executor
289
295
// i.e networkExecutor
@@ -337,6 +343,11 @@ private void doNetworkCall(boolean forceRefresh) {
337
343
// Store the prefs to persist the result of the previous step.
338
344
insertOrUpdatePrefs (prefs );
339
345
346
+ // Update cachedFID, if FID is successfully REGISTERED and persisted.
347
+ if (prefs .isRegistered ()) {
348
+ cachedFid = prefs .getFirebaseInstallationId ();
349
+ }
350
+
340
351
// Let the caller know about the result.
341
352
if (prefs .isErrored ()) {
342
353
triggerOnException (prefs , new FirebaseInstallationsException (Status .BAD_CONFIG ));
@@ -493,6 +504,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
493
504
case AUTH_ERROR :
494
505
// The the server refused to generate a new auth token due to bad credentials, clear the
495
506
// FID to force the generation of a new one.
507
+ cachedFid = null ;
496
508
return prefs .withNoGeneratedFid ();
497
509
default :
498
510
throw new IOException ();
@@ -504,6 +516,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
504
516
* storage.
505
517
*/
506
518
private Void deleteFirebaseInstallationId () throws FirebaseInstallationsException , IOException {
519
+ cachedFid = null ;
507
520
PersistedInstallationEntry entry = getMultiProcessSafePrefs ();
508
521
if (entry .isRegistered ()) {
509
522
// Call the FIS servers to delete this Firebase Installation Id.
@@ -519,7 +532,6 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
519
532
"Failed to delete a Firebase Installation." , Status .BAD_CONFIG );
520
533
}
521
534
}
522
-
523
535
insertOrUpdatePrefs (entry .withNoGeneratedFid ());
524
536
return null ;
525
537
}
0 commit comments