Question

Per this question Why do my Firebase 'child_added' events show up out-of-order? it seems that Firebase makes no garauntees about all clients seeing additions to a list in the same order. Is there some way to force Firebase to use strict ordering and only see local events once they have been confirmed by the server?

Was it helpful?

Solution

The items could appear out of order because it's a real-time distributed system. If multiple records arrive to the server, their keys or priorities could cause them to be inserted before a record that has already been added. Multiply this by 100 users all working in data that is at a different state on their local machine, and it makes sense that ordering is fluid as things move or shift in the list.

However, you can obtain a "snapshot" of the data using the value event, and iterate the items in order using forEach to guarantee the order.

var fb = new Firebase(URL);

// obtain a snapshot of data
fb.once('value', function(snap) {

   // iterate the values in order
   snap.forEach(function(childSnap) {
      console.log(childSnap.name(), childSnap.getPriority(), childSnap.val());
   });

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top