How to know when a Firebase request has ended
I want to collect the last 10 items from my datastore. I suppose I should be able to do this with .child()
and .limitToLast()
, which emits an event every time a result is added to the child.
I don't necessarily know if there are only 10 items, so a simple counter won't work.
How do I know when Firebase has finished producing results?
Sample search code:
var plots = [];
firebaseDatastore.child("plots").orderByChild("unicode-timestamp").limitToLast(10).on("child_added", function(snapshot) {
// Add the new item to the list
plots.push(snapshot.val());
});
I need to know when the last graph was added, whether or not it reached the limit.
source to share
Firebase request never ends. It keeps tracking (in your case) the graphs by unicode-timestamp and keeps a "window" of the last 10 graphs.
So:
- child_added: timestamp1
- child_added: timestamp2
- ...
- child_added: timestamp9
- child_added: timestamp10
And then when you add another plot:
- child_removed: timestamp1
- child_added: timestamp11
If you are not going to use this behavior, you have two options:
- use event
value
and thensnapshot.forEach
over child nodes - save the counter and
off
your listener when you reach the number of babies you expect.
source to share