Reputation: 1000
The following code produces an endless loop of repeated child_added, though I never add a child. The nodes are removed. I just loop forever. How can I fix this?
//notifications branch is pre-populated when I get here:
notifications.startAt(null).on('child_added', processNotification, logError);
function processNotification(notification)
{
removeNotification(notification);
}
function removeNotification(notification)
{
var child = notifications.child(notification.name());
console.log("REMOVING", child.toString());
child.remove();
}
What I log is the same four nodes (A,B,C,D) being removed:
REMOVING https://my.firebaseio.com/experimental/-JVMi0_4poXfOWUt5g49 - A
REMOVING https://my.firebaseio.com/experimental/-JVMi1Y_bFwZAkffRel4 - B
REMOVING https://my.firebaseio.com/experimental/-JVMi2lyhKj8z27ik71x - C
REMOVING https://my.firebaseio.com/experimental/-JVMhzazdgHYAstqxu8L - D
REMOVING https://my.firebaseio.com/experimental/-JVMi0_4poXfOWUt5g49 - A
REMOVING https://my.firebaseio.com/experimental/-JVMi1Y_bFwZAkffRel4 - B
REMOVING https://my.firebaseio.com/experimental/-JVMi2lyhKj8z27ik71x - C
REMOVING https://mu.firebaseio.com/experimental/-JVMhzazdgHYAstqxu8L - D
...
Upvotes: 0
Views: 1087
Reputation: 1000
Ultimately, I just dropped the query and went with normal processing, dropping the "startAt(null)":
notifications.on('child_added', processNotification, logError);
It's not obvious that the behavior will be different from the documentation, but the startAt(null) does not produce the same behavior (though in thoery it should). I would love to see this better documented.
Upvotes: 1
Reputation: 599716
I was able to reproduce the problem. I think it is caused by the fact that the Query
tries to keep items in its "window" when you remove one.
You can work around this problem by using a once('value'
instead of on('child_added'
, like this:
notifications.startAt(null).once('value', processNotifications);
function processNotifications(notifications)
{
notifications.forEach(function(notification) {
notification.ref().remove();
});
}
Since the value
only fires once
, it will not be interfering with the remove
operation.
Upvotes: 1