Firebase.ServerValue.TIMESTAMP在侦听器和实际添加数据的客户端之间未同步 [英] Firebase.ServerValue.TIMESTAMP not synched between listeners and the client that actually adds data

查看:118
本文介绍了Firebase.ServerValue.TIMESTAMP在侦听器和实际添加数据的客户端之间未同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  var fb = new Firebase('https://xxxxxxxxxxx.firebaseio.com /测试'); 

fb.limitToLast(1).on('child_added',function(snap){
console.log('key',snap.key());
console .log('val',snap.val());
});

fb.push({
date_now:Firebase.ServerValue.TIMESTAMP
});

如果我用这个脚本打开两个选项卡,那么实际推送数据的选项是 local 在child_added回调中的时间戳,而刚刚侦听的另一个选项卡获取正确的服务器生成的一个。据我所知,这样做是为了排除往返并节省带宽。

但是对于我的任务来说,这种行为是不可接受的。



这是推送者的console.log:

  key -K59mrvEUhTaoNIQQoA4 
val Object {date_now:1449732570832}

相当于仪表板中显示的服务器数据):

$ p $ 键-K59mrvEUhTaoNIQQoA4
对象{date_now:1449732571759}


解决方案

Firebase会为该写入操作触发两个本地事件:


  1. 立即使用本地时间戳激发一个 child_added 事件)
  2. 它随后在服务器指定的时间触发带有实际时间戳的 child_changed 事件。

所以你可以通过监听这两个事件来解决问题:

  var fb = new Firebase('https://xxxxxxxxxxx.firebaseio.com/test'); 

var query = fb.limitToLast(1);
query.on('child_added',function(snap){
console.log('key',snap.key());
console.log('val',snap。 val());
});
query.on('child_changed',function(snap){
console.log('key',snap.key());
console.log('val',snap。 val());
});

fb.push({
date_now:Firebase.ServerValue.TIMESTAMP
});

一般而言,建议您处理所有子元素_ * 事件,而不仅仅是 child_added 。可能有更多的原因,为什么服务器必须更新或删除值,以纠正本地事件。

如果你喜欢有一个单一的回调/事件处理程序,你还可以监听事件:

  var query = fb。 limitToLast(1); 
query.on('value',function(snap){
snap.forEach(function(child){
console.log('key',child.key());
console.log('val',child.val());
});
});

您将注意到 forEach()在回调中。


Here is the simplest example possible:

var fb = new Firebase('https://xxxxxxxxxxx.firebaseio.com/test');

fb.limitToLast(1).on('child_added', function(snap) {
    console.log('key', snap.key());
    console.log('val', snap.val());
});

fb.push({
    date_now: Firebase.ServerValue.TIMESTAMP
});

If I open two tabs with this script, the one that actually pushes data gets local timestamp in child_added callback and the other tab that just listens gets proper server-generated one. As far as I understand it's done to exclude round-trip and save bandwidth.

But for my task this behaviour is unacceptable. How can I overcome it?

This is the console.log from pusher:

key -K59mrvEUhTaoNIQQoA4
val Object {date_now: 1449732570832}

and listeners (equals to server data seen in dashboard):

key -K59mrvEUhTaoNIQQoA4
val Object {date_now: 1449732571759}

解决方案

Firebase fires two local events for that write operation:

  1. it immediately fires a child_added event with the local timestamp (corrected for your expected offset to the server)
  2. it later fires a child_changed event with the actual timestamp as the server specified it.

So you can solve the problem by listening for both events:

var fb = new Firebase('https://xxxxxxxxxxx.firebaseio.com/test');

var query = fb.limitToLast(1);
query.on('child_added', function(snap) {
    console.log('key', snap.key());
    console.log('val', snap.val());
});
query.on('child_changed', function(snap) {
    console.log('key', snap.key());
    console.log('val', snap.val());
});

fb.push({
    date_now: Firebase.ServerValue.TIMESTAMP
});

In general it is recommended to handle all child_* events and not just child_added. There may be more reasons why the server has to update or remove the value, to correct for the local event.

If you prefer having a single callback/event handler, you can also listen for the value event:

var query = fb.limitToLast(1);
query.on('value', function(snap) {
    snap.forEach(function(child) {
        console.log('key', child.key());
        console.log('val', child.val());
    });
});

You'll note the use of forEach() in the callback.

这篇关于Firebase.ServerValue.TIMESTAMP在侦听器和实际添加数据的客户端之间未同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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