@@ -67,7 +67,8 @@ 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
-
70
+ /* FID of this Firebase Installations instance. Cached after successfully registering and
71
+ persisting the FID locally. NOTE: cachedFid resets if FID is deleted.*/
71
72
private String cachedFid = null ;
72
73
73
74
@ GuardedBy ("lock" )
@@ -217,11 +218,7 @@ String getName() {
217
218
public Task <String > getId () {
218
219
preConditionChecks ();
219
220
TaskCompletionSource <String > taskCompletionSource = new TaskCompletionSource <>();
220
- if (cachedFid != null ) {
221
- taskCompletionSource .trySetResult (cachedFid );
222
- } else {
223
- taskCompletionSource .trySetResult (doGetId ());
224
- }
221
+ taskCompletionSource .trySetResult (doGetId ());
225
222
return taskCompletionSource .getTask ();
226
223
}
227
224
@@ -290,6 +287,9 @@ private void triggerOnException(PersistedInstallationEntry prefs, Exception exce
290
287
}
291
288
292
289
private String doGetId () {
290
+ if (cachedFid != null ) {
291
+ return cachedFid ;
292
+ }
293
293
PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe ();
294
294
// Execute network calls (CreateInstallations) to the FIS Servers on a separate executor
295
295
// i.e networkExecutor
@@ -343,6 +343,11 @@ private void doNetworkCall(boolean forceRefresh) {
343
343
// Store the prefs to persist the result of the previous step.
344
344
insertOrUpdatePrefs (prefs );
345
345
346
+ // Update cachedFID, if FID is successfully REGISTERED and persisted.
347
+ if (prefs .isRegistered ()) {
348
+ cachedFid = prefs .getFirebaseInstallationId ();
349
+ }
350
+
346
351
// Let the caller know about the result.
347
352
if (prefs .isErrored ()) {
348
353
triggerOnException (prefs , new FirebaseInstallationsException (Status .BAD_CONFIG ));
@@ -460,7 +465,6 @@ private PersistedInstallationEntry registerFidWithServer(PersistedInstallationEn
460
465
461
466
switch (response .getResponseCode ()) {
462
467
case OK :
463
- cachedFid = response .getFid ();
464
468
return prefs .withRegisteredFid (
465
469
response .getFid (),
466
470
response .getRefreshToken (),
@@ -512,6 +516,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
512
516
* storage.
513
517
*/
514
518
private Void deleteFirebaseInstallationId () throws FirebaseInstallationsException , IOException {
519
+ cachedFid = null ;
515
520
PersistedInstallationEntry entry = getMultiProcessSafePrefs ();
516
521
if (entry .isRegistered ()) {
517
522
// Call the FIS servers to delete this Firebase Installation Id.
@@ -527,7 +532,6 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
527
532
"Failed to delete a Firebase Installation." , Status .BAD_CONFIG );
528
533
}
529
534
}
530
- cachedFid = null ;
531
535
insertOrUpdatePrefs (entry .withNoGeneratedFid ());
532
536
return null ;
533
537
}
0 commit comments