Factory Reset #268
-
|
I was struggling to 'update' a Factory singleton, in this case a Firestore instance . I think I figured it out but was hoping for confirmation.. e.g. I don't think there's a need to re-register in Step 3b? extension Container { public var firestore: Factory { self { Firestore.firestore() }.singleton }
2a) Auth.auth().signOut() 3a) Container.shared.firestore.reset() 4a) FirebaseApp.configure()
Does all this look ok? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
|
What may or may not be an issue is that resetting the cache only ensures that the next injected value that's asked for gets a new value. Any previously injected or resolved values will still exist, and will not be replaced. Instead of attempting to reset singleton caches, it's usually better to create a manager that tracks the current instance. One that can be cleared as needed. class FirestoreManager {
private(set) var current: Firestore?
func logout() throws {
// do whatever
current = nil
}
}This way anyone who's asked for a FIrestoreManager still has one, and as long as they always go through Even better, if possible, would be to hide current and funnel all access through the manager's functions. |
Beta Was this translation helpful? Give feedback.
-
|
thanks for the rapid response! in Step 5 above I manually update the previously injected value (after removing existing firestore listeners so there are no dangling resolved values): Container.shared.myDataRepo.resolve().firestore = Container.shared.firestore() but yes I see this is non-reactive yuckiness so for your suggestion, do you mean: extension Container { public var firestoreManager: Factory< FirestoreManager> { self { FirestoreManager() }.singleton }}
public class AccRepo: ObservableObject {
@Injected(\. firestoreManager) private var firestoreManager: FirestoreManager
someFunc() {
firestoreManager.current.someFireFunc()
}
} |
Beta Was this translation helpful? Give feedback.
-
|
awesome thanks Michael, so much better. when i find the time i'm still def going to try out Navigator |
Beta Was this translation helpful? Give feedback.
-
|
ok tested it and works like a charm.. here are the order of operations i'm using for firebase signout, clearPersistence, reinstantiate: 1a) remove all firestore listeners 2a) FirebaseApp.configure()
|
Beta Was this translation helpful? Give feedback.
What may or may not be an issue is that resetting the cache only ensures that the next injected value that's asked for gets a new value. Any previously injected or resolved values will still exist, and will not be replaced.
Instead of attempting to reset singleton caches, it's usually better to create a manager that tracks the current instance. One that can be cleared as needed.
This way anyone who's asked for a FIrestoreManager still has one, and as long as they always go through
currentyou can be sure they're talking to the right one.E…