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

I've been doing that, but we lose the auto-adding of $key, $value, $exists, etc.

Obviously we could create similar code to perform the same data transformation, but I'm looking to avoid re-writing angularfire-specific code that will be fixed shortly.

So just hanging on until the fix is released.

Thanks though!

rosslavery avatar Nov 14 '16 15:11 rosslavery

I think this will fix the issue I'm having also. I have an app where I'm adding values to a list using a subscription. It works fine on the initial load of the component, but if I leave the component and comeback, when I add a value to the list, it adds two subsequently. It seems that the subscription call is called twice also.

lbecker34 avatar Nov 24 '16 07:11 lbecker34

For my case, I unsubscribe the subscription call(s) on the component destroy (onDestroy()).

johnqiuwan avatar Nov 24 '16 22:11 johnqiuwan

I seemed to have fixed the problem from this thread https://github.com/angular/angularfire2/issues/377 by calling first().

import 'rxjs/add/operator/first'; this.items.first().subscribe(snapshots => console.log(snapshots));

from @katowulf

mangopop avatar Dec 13 '16 09:12 mangopop

@davideast @katowulf any update on this? We're close to launching a production app that is still affected by this issue. We've been using .debounce(somesmallnumber) as a workaround for now, but this one has been open for nearly 3 months now.

Not meaning to harass, just more concerned about the project's....velocity. I know you guys are juggling many different ____fire libraries though.

rosslavery avatar Dec 15 '16 16:12 rosslavery

@rosslavery @maxmumford Super sorry about the delay on this issue. I've been slammed since switching roles. I had it fixed awhile back but it broke several tests, so there was a lot of plumbing work involved.

I just merged this in with #735, will get a @next release out soon. I would love your feedback on it if you have the time.

davideast avatar Dec 23 '16 23:12 davideast

Thanks, It's working for my app.

hmica avatar Dec 26 '16 08:12 hmica

The @next release also fixed it for us. Thanks @davideast!

rosslavery avatar Dec 28 '16 13:12 rosslavery

Thanks. Works for us as well. Happy new year and thank you for the great work.

urm47 avatar Jan 01 '17 09:01 urm47

Hey all,

i'm encountering the same issue - and i didn't understand the solution (sorry, i'm a rookie). Just to treat everything as objects..? How can I get the "@next" release?

thanks for your patience (:

shaharido1 avatar Jan 02 '17 22:01 shaharido1

@shaharido1, just run npm i angularfire2@next --save

ykravv avatar Jan 02 '17 23:01 ykravv

beta.7 haven't solved it for me, I am doing something like this:

this.af.database.object('data').subscribe((data) => {
this.af.database.list('list/+data).subscribe((myList) => {
this.myList = myList;
});
});

this.myList shows only the first item in the list

theunreal avatar Jan 16 '17 14:01 theunreal

Thanks @davideast , the @next release fixes the issue on my project!

johnqiuwan avatar Jan 16 '17 22:01 johnqiuwan

@theunreal I had this issue with beta6. Beta-7 did resolve it Thanks @davideast

anooprkj2e avatar Jan 25 '17 18:01 anooprkj2e

@davideast thanks for releasing this fix. I'm not using AF2 anymore as the project is now complete, but from everybody else's comments it looks like your changes did the trick!

maxmumford avatar Jan 25 '17 18:01 maxmumford

I have this very simple observable

this.items = angularFire.database.list(databaseUri);

that my component subscribes to. Going from the list page to the details page and back again still produces the problem describes above.

The first time I enter the list of users I get this log

[User, User, User]

Coming back to the list page gives me this:

[User, User, User]
[User, User, User, User]
[User, User, User, User, User, User]

It is always reproducible the first time. Going back and forth between list and details afterwards results in diffrent results.

Adding the .first() operator does solve the problem.

I'm using: "angularfire2": "^2.0.0-beta.7"

bregnvig avatar Jan 28 '17 09:01 bregnvig

@bregnvig Can you create a plnkr reproducing this issue?

davideast avatar Jan 28 '17 18:01 davideast

Also, this issue is closed, so starting a new thread with a complete repro and details will be appropriate.

katowulf avatar Jan 29 '17 15:01 katowulf

Now it works fine, don't miss "npm i angularfire2@next --save" guys before using .first() .take(1)

da45 avatar Feb 08 '17 09:02 da45

Just subscribe inside the ngOnInit() method

alan-redpanthers avatar Jul 18 '17 07:07 alan-redpanthers