I have the following login function that I want to use just once to login as oppose to sit there an listen for data forever. So I am looking for the right time to call the unsubscribe method. At the moment, I am calling the unsubscribe method right after I get a result or error. This would make sense as the observable is no longer required.
However... what happens if I have a really slow internet and here is what happens.
The code executes the observable and waits for a data to come back from firebase (Say for 1 minute for arguments sake). During this wait period, say if someone modifies this entry in firebase, I believe firebase will think, "hey someone is still listening and there is a change, so I better emmit the update to this person"
So at this point, I would be waiting for two sets of data to come back, the first one and the updated one.
So would I get two data printed out of the console or will this not happen?
onLogin() {
// loginWithEmailPassword returns firebase.promise<AuthState>
this.userService.loginWithEmailPassword(this.loginForm.value.email, this.loginForm.value.password)
.then(data => {
// if the user is logged in, go and retreive user information
// getUserInformation returns Observable<any>
let subscription_getUserInformation = this.userService.getUserInformation(data.uid)
.subscribe(result => {
// user information received
console.log(result)
// remove Subscription
subscription_getUserInformation.unsubscribe();
}, error => {
// something went wrong
subscription_getUserInformation.unsubscribe();
})
}, error => {
// something went wrong
});
}