Skip to content

Instantly share code, notes, and snippets.

@kshivang
Last active November 18, 2023 19:51
Show Gist options
  • Save kshivang/2b6561c0574a4031ee0e40643e589b8f to your computer and use it in GitHub Desktop.
Save kshivang/2b6561c0574a4031ee0e40643e589b8f to your computer and use it in GitHub Desktop.
Switch/Refresh publisher on Firebase Auth new login simple one-liner for rever-ai/CombineFirebase
func onLoginRefresher<T>(_ publisherGetter: @escaping () -> AnyPublisher<T, Never> ) -> AnyPublisher<T, Never> {
Auth.auth().stateDidChangePublisher
.filter { $0 != nil } // User LoggedIn
.map { _ in publisherGetter() }
.switchToLatest()
.eraseToAnyPublisher()
}
// e.g.: turn userDataPublisher to onLoginRefresher { userDataPublisher() } and done
//
// onLoginRefresher { userDataPublisher() }
// .receive(on: RunLoop.main)
// .sink { userData -> do Someting }
// .store(in: &cancelBag)
@kshivang
Copy link
Author

kshivang commented Sep 30, 2020

rever-ai/CombineFirebase is the major dependency

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment