angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Multiple subscriptions on a FirebaseListObservable

Open maxmumford opened this issue 10 years ago • 42 comments

Hi all,

When querying fb with a FirebaseListObservable like so:

let items = this.angularFire.database.list('/items', query{...}).subscribe(x => console.log(x));

the output will be something like:

[Object, Object, Object, Object]

However when attaching two subscribers, each subscriber is triggered each time an item is added to the results array. for example:

let items = this.angularFire.database.list('/items', query{...});
items.subscribe(x => console.log('Subscriber 1: ', x));
items.subscribe(y => console.log('Subscriber 2: ', y));

// output:
Subscriber 1: []
Subscriber 1: [Object]
Subscriber 1: [Object, Object]
Subscriber 1: [Object, Object, Object]
Subscriber 1: [Object, Object, Object, Object]
Subscriber 2: []
Subscriber 2: [Object]
Subscriber 2: [Object, Object]
Subscriber 2: [Object, Object, Object]
Subscriber 2: [Object, Object, Object, Object]

I would expect both subscribers to be called just once with the final array result. Is this an error in my implementation, or by design?

My goal here is to search a list and do something if no items are returned from the search, ie:

let items = this.angularFire.database.list('/items', query{...});
items.subscribe(x => console.log('Subscriber 1: ', x));
items.subscribe(x => {
    if(x.length == 0)
        // do something
});

I would create a plunker for this but it seems the af2 version used by plunker has a problem with facebook auth (but let's leave that for another issue ;) ):

http://plnkr.co/edit/4IbB5IvfkBYcj2iVAIM1?p=preview

maxmumford avatar Sep 28 '16 09:09 maxmumford

Is it possible that the bug has come back in 4.0.0-rc0 and 4.0.0-rc1?

mastronaute avatar Jul 25 '17 13:07 mastronaute

I am having this issue with the current latest

jimmykane avatar Dec 16 '18 13:12 jimmykane