为什么在“价值”事件之后Firebase事件“child_added”被触发? [英] Why does the Firebase event 'child_added' fire after the 'value' event?

查看:137
本文介绍了为什么在“价值”事件之后Firebase事件“child_added”被触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Firebase 文档
$ b


值事件总是触发上次,并且保证包含之前发生的其他事件的更新

下面是一个简单的例子( jsbin )其中 child_added 在之前触发 。此行为已通过当前最新的Firebase版本(2.3.1)得到证实: /reform.firebaseio.com);一次('价值',功能(快照){

snapshot.forEach(功能(宠物){
console.log( Pet:+ pet.key());

pet.ref()。child('food')。once('value',function(foods){
console.log ('value event called'+ pet.key());
));

pet.ref()。child('food')。once('child_added',function (food){
console.log('child_added event called'+ pet.key());
});
});
});

在这个例子中,控制台日志将是:

 宠物:猫
宠物:狗
值事件叫做猫
child_added事件叫做猫
值事件叫做狗
child_added event for dog

为什么 child_added 事件在这种情况下最后触发?这是否违反了每个文档的保证?

解决方案

总结评论和Firebase支持中的出色反馈: ()而不是中最有意义的是$ p
$ b code> once()
来注册事件监听器。在原始文章示例中,并引用Firebase支持:< ()回调是注册的,当注册once()回调时,它是根据on()回调进行排序的。回页首返回页首返回页首返回页首返回页首返回页首once()回调被触发后,会自动取消注册。即使事件的执行是按照一定的顺序完成的(由于JavaScript是单线程的),它们是相互分开计算的。


弗兰克对这个例子的更正表明了这一点。


  • 修改的例子再次打破了保证,因为(Firebase支持):

    lockquote

    数据已经在本地客户端上。所以一旦你运行ref.child('pets')。on()并且发生回调,/ pets下的所有数据都已经被检索到客户端。现在在回调处理中,您正在向现有数据添加额外的回调。当回调被添加时,客户端库立即发射回调,而不用等待第二个注册,因为所有的数据都可用。


    因为我想在这种情况下强制保证数据是本地的,所以我只需在的值之前注册 child_added / code>监听器,如修改示例 a>。



  • According to the Firebase documentation:

    Value events are always triggered last and are guaranteed to contain updates from any other events which occurred before that snapshot was taken.

    Here is a simple example (jsbin) where child_added fires before value. This behavior was confirmed using the currently latest Firebase version (2.3.1):

    var ref = new Firebase("https://reform.firebaseio.com");
    ref.child('pets').once('value', function(snapshot) {
    
        snapshot.forEach(function(pet) {
            console.log("Pet: " + pet.key());
    
            pet.ref().child('food').once('value', function (foods) {
                console.log('value event called for ' + pet.key());
            });
    
            pet.ref().child('food').once('child_added', function (foods) {
                console.log('child_added event called for ' + pet.key());
            });
        });
    });
    

    In this example, the console log will be:

    Pet: cat
    Pet: dog
    value event called for cat
    child_added event called for cat
    value event called for dog
    child_added event called for dog
    

    Why does the child_added event fire last in this case? Does this not violate the guarantee per the documentation?

    解决方案

    To summarize the excellent feedback in the comments and by Firebase Support:

    1. It makes most sense here to use on() instead of once() to register the event listener. In the example of the original post, and to quote Firebase Support:

      The on() callback is registered, and when the once() callback is registered it is ordered in reference to the on() callback. Once the once() callback is fired, it's automatically deregistered. Even though the execution of the events are done in a certain order (due to javascript being single threaded), they are being calculated separately from each other.

      Frank's correction to that example shows this in action.

    2. The modified example again breaks the "guarantee" because (Firebase Support):

      The data is already locally on the client. So once you have run ref.child('pets').on() and the callback happens, all the data under /pets has been retrieved to the client. Now in the callback processing, you are adding additional callbacks to the existing data. When the callback is being added, the client library is immediately firing the callback without waiting for the second one to be registered since all the data is available.

      Since I would like to enforce the guarantee in this case where the data is local, I simply register the child_added listener before the value listener, as demonstrated in the correction to the modified example.

    这篇关于为什么在“价值”事件之后Firebase事件“child_added”被触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆