My goal is to subscribe to snapshotChanges() of a firestore collection when entering a view and unsubscribing from that when leaving the view.
Problem: The subscription works fine, unsubscribing does not work.
I followed the answer in this angularfire git thread.
Here is part of my code:
subscriptions: any[] = [];
ionViewWillEnter() {
console.log('View will enter...');
this.subscriptions.push(
this.firestore.collection('/somecollection/').snapshotChanges().subscribe(docs => {
// Functions that use docs and
// more subscriptions of the same type nested in here
console.log('Subscription #', someUniqueSubIdentifier);
})
);
}
goToOtherPage() {
this.router.navigate(['/some-other-page']);
}
ionViewWillLeave() {
console.log('View will leave...');
for (const sub of this.subscriptions) {
sub.unsubscribe();
}
console.log('All subscriptions: ', this.subscriptions);
}
The problem is that the subscription doesn't work here. I get the following prints after leaving the view for the first time and coming back to it.
View will enter...
Subscription #1 //(once and for every occurring change as expected)
View will leave...
All subscriptions: (3) [Subscriber, Subscriber, Subscriber]
// (User pushed changes to the collection in the meantime)
View will enter...
Subscription #1 (not expected)
Subscription #2 (expected)
Can anybody help? Why does unsubscribe() not work here?
My ionic and angular versions:
Ionic CLI: 6.12.1
Ionic Framework: @ionic/angular 5.3.4
"@angular/fire": "^6.0.4"