Choosing between legacy setPersistenceEnabled and FirebaseDatabaseSettings.Builder for persistence in Firebase Realtime Database SDK 10.x
0 reputation · 18 Jul 2023, 06:34 UTC
0 reputation · 18 Jul 2023, 06:34 UTC
Goal: Enable disk‑based persistence for Firebase Realtime Database while supporting apps that use multiple FirebaseApp instances or custom FirebaseApp configurations.
Constraint: In SDK 10.x the persistence flag moved from FirebaseDatabase.getInstance().setPersistenceEnabled(...) to FirebaseDatabaseSettings.Builder.setPersistenceEnabled(...), but the legacy method remains available for backward compatibility. It is unclear whether the two approaches behave identically when a non‑default FirebaseApp is used, and whether they differ in flush latency or cache eviction under intermittent connectivity.
Questions: Does invoking the legacy setPersistenceEnabled on a FirebaseDatabase obtained from a secondary FirebaseApp share the same persistence cache as the default app? Does the builder‑based approach require a separate FirebaseDatabase instance per FirebaseApp, and how does that affect memory usage? Are there measurable differences in write‑flush latency or cache eviction behavior between the two methods when the network drops and reconnects repeatedly?
30275 reputation · 18 Jul 2023, 17:59 UTC
For Firebase Realtime Database SDK 10.x, you should transition to FirebaseDatabaseSettings.Builder. While the legacy setPersistenceEnabled(boolean) method remains available for backward compatibility, the Builder pattern is the current standard for initializing database configurations, especially when dealing with multiple FirebaseApp instances.
When using secondary FirebaseApp instances, persistence caches are isolated. Invoking either the legacy method or the Builder on a FirebaseDatabase instance obtained from a secondary app will not share the persistence cache with the default app. Each app instance maintains its own separate disk-based storage to prevent data leakage and collision between different Firebase projects.
The Builder-based approach does not inherently require a separate FirebaseDatabase instance beyond what is already required for each FirebaseApp. Memory usage is primarily driven by the size of the local cache and the number of active listeners, regardless of whether you used the Builder or the legacy method to enable persistence. However, using the Builder allows you to configure multiple settings atomically before the database instance is fully initialized.
There are no documented differences in write-flush latency or cache eviction logic between the two methods. Both trigger the same underlying disk-persistence engine. The behavior during intermittent connectivity—such as how the SDK handles the queue of pending writes and subsequent synchronization upon reconnection—is consistent across both configuration paths.
FirebaseDatabaseSettings object using the Builder before any database references are accessed.FirebaseApp.IllegalStateException, which occurs if persistence is toggled after the database has already started.// Example for SDK 10.x
FirebaseDatabaseSettings settings = new FirebaseDatabaseSettings.Builder()
.setPersistenceEnabled(true)
.build();
FirebaseDatabase database = FirebaseDatabase.getInstance(secondaryApp, settings);
To verify isolation, you can inspect the application's internal storage directories; separate FirebaseApp instances will utilize distinct cache folders. Note that mixing legacy calls and Builder settings on a single instance can lead to undefined behavior and should be avoided.
Diagnostic Detail Required: Are you initializing these FirebaseApp instances dynamically at runtime or statically during application launch? This determines if you need to implement a synchronization lock to prevent premature database access before the Builder settings are applied.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.