Most applications treat Firebase data as their model. Therefore, when there is an update, they call ref.set() (or another mutator function), and then the update returns to their application through the on() event. React / Flux aficionados know this as a unidirectional data stream , others may know it as Segmentation of command line response requests .
But there really are cases when the model is already updated, and therefore you want to ignore the event from Firebase if you are the one who raised it.
There is no API for not receiving abstracts caused by self-issuing. Instead, you will have to โrememberโ the data sent to Firebase and filter it in your on() handler.
An Android drawing sample from Firebase stores a list of segments that it sends to Firebase , and then ignores these segments in its onChildAdded handler . It uses push identifiers to identify line segments, and they are generated on the client side, so they can use those that track segment identification.
JavaScript Usage Example:
var pendingChildIds = []; // Push ids of nodes we've sent to the server, but haven't received in `on()` yet // this code is in your UI event handler, or whatever triggers the needs to update your Firebase data var newChild = ref.push(); pendingChildIds.push(newChild.key()); newChild.set( { property1: 'value1', property2: 3.14 }, function(error) { // the write operation has completed, remove the child id from the list of pending writes pendingChildIds.splice(pendingChildIds.indexOf(newChild.key()); } ); // this is the event handler, using child_added in this case ref.on('child_added', function(snapshot) { if (!pendingChildIds.contains(snapshot.key())) { // this is a child that we DIDN'T generate } });
Frank van puffelen
source share