3

I have an array = [ 'something', 'other' ]

And I want to retrieve only the values of those 2 ids from Firebase, which contains more than 2 items ( potentially millions ), but if I do this:

var questionRef = new Firebase(fireBaseURL+"/morethanamillionitems/");

loadUID.once('value', function (dataSnapshot) {

    dataSnapshot.forEach(function(childSnapshot) {  // Firebase method

      console.log(dataSnapshot.numChildren()); // potentially outputs 1.000.000 +
      var uid = childSnapshot.name();
      var childData = childSnapshot.val();
      console.log(uid.indexOf('something'));
      result.push(uid)
    });
}

I first basically load the whole database, which is not that efficient

Now I could do:

array.forEach(key, function() {
  var questionRef = new Firebase(fireBaseURL+"/morethanamillionitems/"+key);
  refID = questionRef.val();
  result.push(refID);
})

Or maybe:

questionRef = new Firebase(fireBaseURL+"/morethanamillionitems/");
array.forEach(key, function() {
  if ( questionRef.child(key) !== null ){
     refID = questionRef.val();
     result.push(refID);
  }
})

The last one seems the nicest, the previous one seems a bit expensive on the old RAM.

However, I apparently have to call questionRef.once('value', function(){}) each time, hence already loading the whole document-root...

Or am I misunderstanding how Firebase handles these requests? is the .numChildren() just an answer directly from the server?

Is the .forEach actually remotely executed?

I'm wondering if there is any other way to reduce traffic per request. Which brings me to another question: it seems that firebase searches locally first, but eventually will search remotely, but it's not clear when this exactly happens. Does it periodically check if something has changed? Or will that only happend when I use .on() and not .once(). Or am I using the wrong backend for this purpose? Any other suggestions? I tried hood.ie which is still very beta, looked at Parse but firebase seemed to have the simplicity I need.

(sorry for the sloppy syntax, but you can see what I intended)

[update] I now have this:

load: function(uids){
     var FB = new Firebase(URL);
        uids.map(function(uid) {
          var currentRef = FB.child( uid+"/_current" );
          currentRef.once('value', function (each) {
              eachVal = each.val()
              if (eachVal !== null){
                var localSave = {};
                localSave[uid] = eachVal;
                this.saveLocal(localSave)
              } else {
                console.error("Not found: [%s]", uid)
            }}, function (err) {  });
        });
    }

But I'm still wondering when the request actually happens, on .child()? or in .once() and if the latter, what is the use of .child() exactly? It seems it's only used for referencing.

Then the second thing, if I want to retrieve an array of a hundred items, this would still mean a hundred seperate requests? or does Firebase have a way of collecting requests and then send them in a batch? In that last case .once would be a more 'conservative' option for initial retrieval, then later you could attach a .on listener if you need real-time updates.

TrySpace
  • 2,233
  • 8
  • 35
  • 62
  • possible duplicate of [Firebase: Query filtering in javascript on() events](http://stackoverflow.com/questions/15730709/firebase-query-filtering-in-javascript-on-events) – Kato May 20 '14 at 17:18
  • 1
    .forEach is a synchronous, local helper function. To achieve a reduced set of ids, create an index as explained in the linked post about Query filtering. Cheers! – Kato May 20 '14 at 17:19

0 Answers0