Reputation: 384
This is my code, I can't see why my code is executed 3 times.
advertenciaMantto() {
this.afs.doc('Core/Programador').valueChanges().subscribe(async (data) => {
await this.afs.firestore.doc(`Core/Programador`).get().then(async data => {
if (data.data().manager.prevencion) {
this.presentAlert();
}
});
});
}
Can you help me, please?
EDIT
I tried with .pipe(first()) and it works, the problem is that when I make a change in my firestore data, it does not execute because it has already been executed once.
Upvotes: 1
Views: 128
Reputation: 384
Here is my code:
async advertenciaMantto() {
await this.afs.firestore.doc(`Core/Programador`).get().then(async doc => {
if (doc.data().manager.prevencion) {
this.presentAlert();
} else {
this.alertaSubs = this.afs.doc('Core/Programador').valueChanges().subscribe(async () => {
await this.afs.firestore.doc(`Core/Programador`).get().then(async doc => {
if (doc.data().manager.prevencion) {
this.alertaSubs.unsubscribe();
this.presentAlert();
}
})
});
}
});
}
Upvotes: 0
Reputation: 11
You are using .subscribe()
method which has a different behavior from .get()
the first time a subscriber runs, it always returns no documents, then it run again and return documents from you local cache, and then it runs one more time returning the documents from remote (firebase), and then it starts listening for changes in the documents
Upvotes: 1