In my angular project I am subscribed to a Firestore document like so:
const obs = this.db.collection('someCollection').doc('someDoc').valueChanges().subscribe(x=>console.log(x));
I am only subscribed in one place, there is no async pipes in my template.
When I write directly to Firebase this works correctly, and I only receive one log. When I use the set method from AngularFirebase I only receive one log.
this.db.collection('someCollection).doc('someDoc').set({value: 1});
//obs will log 'value:1'
But when I use the update method I get two logs.
this.db.collection('someCollection').doc('someDoc').update({value: 1});
//obs will log 'value:1' two times.
It seems to me that this should only log one time. Any ideas?
The answer to my problem was that the value was optimistically updating locally (causing the observable to fire), then the value was being pushed to Firestore, and being reread causing the observable to fire again.
The way I figured this out was to turn off the network and the observable only fired once.
This also explains why, when I write directly to Firestore the observable only fires once.
So if you want the observable to only fire once, you have to put a distinctUntilChanged() function on the observable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With