0

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?

Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734
lei lei
  • 1,533
  • 3
  • 15
  • 36

1 Answers1

1

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(...);
Reqven
  • 1,462
  • 1
  • 7
  • 12