Multiple subscriptions on a FirebaseListObservable
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
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!
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.
For my case, I unsubscribe the subscription call(s) on the component destroy (onDestroy()).
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
@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 @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.
Thanks, It's working for my app.
The @next release also fixed it for us. Thanks @davideast!
Thanks. Works for us as well. Happy new year and thank you for the great work.
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, just run npm i angularfire2@next --save
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
Thanks @davideast , the @next release fixes the issue on my project!
@theunreal I had this issue with beta6. Beta-7 did resolve it Thanks @davideast
@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!
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 Can you create a plnkr reproducing this issue?
Also, this issue is closed, so starting a new thread with a complete repro and details will be appropriate.
Now it works fine, don't miss "npm i angularfire2@next --save" guys before using .first() .take(1)
Just subscribe inside the ngOnInit() method