Skip to content

Commit 3c974c4

Browse files
committed
Addressing Rayo's comments.
1 parent 1d8aa16 commit 3c974c4

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
6767
private final Object lock = new Object();
6868
private final ExecutorService backgroundExecutor;
6969
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.*/
7172
private String cachedFid = null;
7273

7374
@GuardedBy("lock")
@@ -217,11 +218,7 @@ String getName() {
217218
public Task<String> getId() {
218219
preConditionChecks();
219220
TaskCompletionSource<String> taskCompletionSource = new TaskCompletionSource<>();
220-
if (cachedFid != null) {
221-
taskCompletionSource.trySetResult(cachedFid);
222-
} else {
223-
taskCompletionSource.trySetResult(doGetId());
224-
}
221+
taskCompletionSource.trySetResult(doGetId());
225222
return taskCompletionSource.getTask();
226223
}
227224

@@ -290,6 +287,9 @@ private void triggerOnException(PersistedInstallationEntry prefs, Exception exce
290287
}
291288

292289
private String doGetId() {
290+
if (cachedFid != null) {
291+
return cachedFid;
292+
}
293293
PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe();
294294
// Execute network calls (CreateInstallations) to the FIS Servers on a separate executor
295295
// i.e networkExecutor
@@ -343,6 +343,11 @@ private void doNetworkCall(boolean forceRefresh) {
343343
// Store the prefs to persist the result of the previous step.
344344
insertOrUpdatePrefs(prefs);
345345

346+
// Update cachedFID, if FID is successfully REGISTERED and persisted.
347+
if (prefs.isRegistered()) {
348+
cachedFid = prefs.getFirebaseInstallationId();
349+
}
350+
346351
// Let the caller know about the result.
347352
if (prefs.isErrored()) {
348353
triggerOnException(prefs, new FirebaseInstallationsException(Status.BAD_CONFIG));
@@ -460,7 +465,6 @@ private PersistedInstallationEntry registerFidWithServer(PersistedInstallationEn
460465

461466
switch (response.getResponseCode()) {
462467
case OK:
463-
cachedFid = response.getFid();
464468
return prefs.withRegisteredFid(
465469
response.getFid(),
466470
response.getRefreshToken(),
@@ -512,6 +516,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
512516
* storage.
513517
*/
514518
private Void deleteFirebaseInstallationId() throws FirebaseInstallationsException, IOException {
519+
cachedFid = null;
515520
PersistedInstallationEntry entry = getMultiProcessSafePrefs();
516521
if (entry.isRegistered()) {
517522
// Call the FIS servers to delete this Firebase Installation Id.
@@ -527,7 +532,6 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
527532
"Failed to delete a Firebase Installation.", Status.BAD_CONFIG);
528533
}
529534
}
530-
cachedFid = null;
531535
insertOrUpdatePrefs(entry.withNoGeneratedFid());
532536
return null;
533537
}

0 commit comments

Comments
 (0)