Angularfire: complete doesnt get fired in FirebaseListObservable.subscribe

Created on 10 Oct 2016  路  4Comments  路  Source: angular/angularfire

I have this angular2 code:

af.database.list('/users').subscribe(
      value=>console.log(value),
      error=>console.log(error),
      ()=>console.log("FIMISHED!!!") //Never gets here
    );

Problems:

  1. As I undersatnd, "Finished" should be printed in the console, but it doesn't.
  2. af.database.list('/users').toPromise().then(......never gets here.......).catch(......never gets here......)...

Questions:

  1. What can I do to make these code work as expected?
  2. How can we convert FirebaseListObservable to Observable (That doesnt contain the extra methods of FirebaseListObservable and use the rxjs implementation for all his methods)?

Most helpful comment

Hi @johnqiuwan ! As @davideast answered, firebase observable will never be completed because it is a real time database and things are changes over time so if you ask for users as u did, u dont ask for them, you subsribe to changes in the users so every time a new user created, this line will be called : console.log(value). If you want to force the observable to finish after you got the first value, do that: someObservalbe.take(1).doSomethingElseLike_toPromise().then(...).

All 4 comments

Hey @stavalfi, I'll be happy to answer your questions!

Complete will not fire unless you manually terminate the Firebase listener. You can do this by calling dispose on the observable, using an operator that disposes, or calling .off() on the reference yourself.

If you're looking to convert to a promise, then I recommend using take:

af.database.list('users').take(1).toPromise();

Hi @davideast thanks for answering so fast!
Thanks very much!!!!

Hello @stavalfi ,

I have the similar issue as you, and how did you solve your issue ?

af.database.list('/users').subscribe(
      value=>console.log(value),
      error=>console.log(error),
      ()=>this.loading = false //Never gets here
    );

Hi @johnqiuwan ! As @davideast answered, firebase observable will never be completed because it is a real time database and things are changes over time so if you ask for users as u did, u dont ask for them, you subsribe to changes in the users so every time a new user created, this line will be called : console.log(value). If you want to force the observable to finish after you got the first value, do that: someObservalbe.take(1).doSomethingElseLike_toPromise().then(...).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

itisparas picture itisparas  路  3Comments

fisherds picture fisherds  路  3Comments

harrylincoln picture harrylincoln  路  3Comments

jteplitz picture jteplitz  路  3Comments

StephenFluin picture StephenFluin  路  3Comments