9

I have rather big data-set, possibly millions of objects and I need to trigger the child_added event and get the last added child. However whenever I start the service it triggers the event once for each initial child in the data-set.

How Can I avoid this behavior?

edit:

The suggested solution does not solve the problem - it is just ignoring it in my opinion. In my specific case I store the unique reference to other object as a name and it's value is other important data. The Priority on the record is set to the specific number in my case time stamp. I use this to retrieve the record to build a timeline so I need the option to save a new object with custom timestamp - not only current. Listener .on() on event child_added fits the need perfectly except fetching all the children upon the start. This in my opinion renders this feature absolutely pointless on any large collection.

webduvet
  • 4,220
  • 2
  • 28
  • 39
  • possible duplicate of [how to discard initial data in a Firebase DB](http://stackoverflow.com/questions/19883736/how-to-discard-initial-data-in-a-firebase-db) – Kato Jul 22 '14 at 19:26
  • yes, the question is similar, but the solution is not there. As a matter of fact that is exactly what I dont want - fetch the entire snapshot... – webduvet Jul 23 '14 at 09:38
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jul 23 '14 at 10:09

1 Answers1

3

You could use limit(1). Then the listener will fire once at the beginning, and then again each time a new child is added.

fireRef.limit(1).on('child_added', function() { /*code*/ });

Timothy Smith
  • 800
  • 1
  • 5
  • 15
  • yes, but please read the edit. this method only works if the new child is added at the bottom of the collection. – webduvet Jul 27 '14 at 11:42
  • As far as I know, Firebase doesn't have a feature which allows what you're trying to do. The only way I know of to achieve what you're looking for is to use priority and a restriction such as `limit(1)`. – Timothy Smith Jul 27 '14 at 21:15