clear local firestore cache? #14596
Replies: 6 comments 1 reply
-
Unfortunately, no, there is no way to clear Firestore's cache without terminating the Firestore instance. Even the workaround you suggested of changing the cache size to zero and back to unlimited won't work because the Firestore settings cannot be changed after they "take effect": firebase-ios-sdk/Firestore/core/src/api/firestore.cc Lines 122 to 129 in f504fae That is, you have to terminate then re-create the Firestore instance to change the maximum cache size. Could I ask why calling Thank you for the question. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the rapid response Denver. Due to the firestore documentation stating clearPersistence "is primarily intended to help write reliable tests" I was wondering if there was a different 'best practice'. And yeah i was hoping to avoid terminate() to avoid changing the Firestore instance dynamically. I'm injecting Firestore using Michael Long's great Factory library and it's making things a bit complicated (I think it's resulting in dangling references, even when using reset() for the factories). So it sounds like I'll figure out changing the Firestore instance dynamically. To avoid the complexity I'm tempted to actually quit my app upon signing out (if Apple allows it) e.g. // ...inside my handleSignOut() function...
do {
try Auth.auth().signOut(); print("Firebase Authentication: Signed out user.")
try await terminateFirestoreAndClearPersistence()
UserDefaults.standard.synchronize()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { exit(0) }} // yikes
catch { print("❌ Error during sign-out or Firestore cleanup: \(error) }
// .........
func terminateFirestoreAndClearPersistence() async throws {
try await Firestore.firestore().terminate(); print("💀 Firestore Terminated.")
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
Firestore.firestore().clearPersistence { error in
if let error { continuation.resume(throwing: error) }
else { continuation.resume(); print("✅ Firestore persistence cleared.") }}}
} |
Beta Was this translation helpful? Give feedback.
-
Thanks for the update. Sorry I don't have a better answer for you. Although clearPersistence is indeed primarily intended to help write reliable tests, it is a fully-supported function that can be used in production apps, if desired. |
Beta Was this translation helpful? Give feedback.
-
ok thanks a bunch for the info that clearPersistence is ok to use and i'll post an update when i figure out how to update the Factory firebase singleton |
Beta Was this translation helpful? Give feedback.
-
ok i was able to reinstantiate Firestore including use of Factory DI.. i incorrectly assumed the Factory reset() would automagically re-inject the new firestore instance into my repo but when i manually did it (step 5) everything came up roses (that is until someone paves over them):
2a) Auth.auth().signOut()
4a) FirebaseApp.configure()
|
Beta Was this translation helpful? Give feedback.
-
just got a suggestion from the man the legend Michael Long himself. instead of resetting the singleton cache just store the current Firestore instance in a custom FirestoreManager() class. so the following rough code shows a much better, reactive approach: class FirestoreManager {
private(set) var currentFirestore: Firestore
init(currentFirestore: Firestore = Firestore.firestore()) { self.currentFirestore = currentFirestore }
func reinstantiateFirebase() async throws { ... }
func signoutTerminateClearPersistence() async throws { ... }
}
extension Container {
var firestoreManager: Factory<FirestoreManager> { self { FirestoreManager() }.singleton }
}
class myClass: ObservableObject {
@Injected(\.firestoreManager) private var firestoreManager: FirestoreManager
someFunc() {
firestoreManager.currentFirestore.someFireFunc()
}
} ....so order of operations would be: 1a) remove all firestore listeners 2a) FirebaseApp.configure()
e.g. in my AuthManager: @Injected(\.firestoreManager) var firestoreManager: FirestoreManager
...
func handleSignOut() {
...
await removeAllFirestoreListeners()
try await firestoreManager.signoutTerminateClearPersistence()
try await firestoreManager.reinstantiateFirebase()
setupAuthListeners()
print("sign out complete")
...
} |
Beta Was this translation helpful? Give feedback.
-
for proper security, after a user signs out via Auth.auth().signOut() I would like to clear the local firestore cache on their device
i would really really prefer to avoid using firestore.terminate() followed by firestore.clearPersistence
so i was wondering if there's a 'built-in' method to clear the local cache without being forced to kill the firestore instance
if not, is there a creative best practice? e.g. turning persistence off and back on, or setting the cache to 0 bytes and then back to unlimited?, etc.
if not, is this on the to-do list?
Beta Was this translation helpful? Give feedback.
All reactions