Skip to content

Commit 1d8aa16

Browse files
committed
Cache FID to avoid multiple disk reads.
1 parent b26d198 commit 1d8aa16

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
6868
private final ExecutorService backgroundExecutor;
6969
private final ExecutorService networkExecutor;
7070

71+
private String cachedFid = null;
72+
7173
@GuardedBy("lock")
7274
private final List<StateListener> listeners = new ArrayList<>();
7375

@@ -215,7 +217,11 @@ String getName() {
215217
public Task<String> getId() {
216218
preConditionChecks();
217219
TaskCompletionSource<String> taskCompletionSource = new TaskCompletionSource<>();
218-
taskCompletionSource.trySetResult(doGetId());
220+
if (cachedFid != null) {
221+
taskCompletionSource.trySetResult(cachedFid);
222+
} else {
223+
taskCompletionSource.trySetResult(doGetId());
224+
}
219225
return taskCompletionSource.getTask();
220226
}
221227

@@ -454,6 +460,7 @@ private PersistedInstallationEntry registerFidWithServer(PersistedInstallationEn
454460

455461
switch (response.getResponseCode()) {
456462
case OK:
463+
cachedFid = response.getFid();
457464
return prefs.withRegisteredFid(
458465
response.getFid(),
459466
response.getRefreshToken(),
@@ -493,6 +500,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
493500
case AUTH_ERROR:
494501
// The the server refused to generate a new auth token due to bad credentials, clear the
495502
// FID to force the generation of a new one.
503+
cachedFid = null;
496504
return prefs.withNoGeneratedFid();
497505
default:
498506
throw new IOException();
@@ -519,7 +527,7 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
519527
"Failed to delete a Firebase Installation.", Status.BAD_CONFIG);
520528
}
521529
}
522-
530+
cachedFid = null;
523531
insertOrUpdatePrefs(entry.withNoGeneratedFid());
524532
return null;
525533
}

0 commit comments

Comments
 (0)