My ionic 5 (Angular) project uses Google firebase database.
This is an example code I used to read data from firebase database. I just want to read the data only once. As valueChanges()
is a observable, I guess I have to unsubscribe the subscription every time when use it.
constructor(db: AngularFireDatabase) {
this.tempsub = db.list('items').valueChanges().subscribe(data=>{...});
this.tempsub.unsubscribe()
}
Do I have to unsubscribe it in my use case? Is there a better solution to read the data from firebase database only once?
You can use the take
operator to configure the desired number of emissions.
Using take(1)
will emit the first value, then complete and unsubscribe.
db.list('items').valueChanges()
.pipe(take(1))
.subscribe(...);