Firebase.ServerValue.TIMESTAMP在即时child_added上返回不同的值 [英] Firebase.ServerValue.TIMESTAMP returns different value on immediate child_added

查看:146
本文介绍了Firebase.ServerValue.TIMESTAMP在即时child_added上返回不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有服务器时间戳的Firebase推送,其中一个 child_added 侦听器已经在监听。



,返回到 child_added 事件的值与事实上存储在Firebase数据库节点中的值不同。随后的读取在同一个节点上,提供了存储在节点上的正确时间。这只是初始的child_added事件,当推送正在发生,正在返回一个错误的时间正确触发。



有没有人观察过这个问题?

  ref = new Firebase(/ some / path); 
ref.on('child_added',function(childData){
console.log('childData.pushTime ='+ childData.pushTime);
});
ref.push({pushTime:Firebase.ServerValue.TIMESTAMP});

console.log输出的内容与实际存储的路径相距300毫秒。

解决方案

您看到的行为是预期的行为。

因为 child_added 事件是在本地触发的,所以使用当前时间确定的 ServerValue.TIMESTAMP 值以及本地时钟与服务器时钟的偏移。但是,无法计算请求到达服务器所需的时间 - 因此时间戳是不同的。



如果您还听 child_changed 事件,一旦写入完成,它将以一个包含正确时间的快照触发。


I have a Firebase Push with Server Timestamp, where a child_added listener is already listening.

Extremely surprisingly, the value returned to the child_added event is ~300ms different from what is in fact stored in the Firebase Database node. Subsequent reads at the same node, provides the 'correct' time as stored at the node. It is only the initial child_added event that is triggered right when the push is occurring, that is returning a wrong time.

Has anyone observed this issue?

ref = new Firebase(/some/path);
ref.on('child_added', function (childData) {
   console.log('childData.pushTime = ' + childData.pushTime);
});
ref.push({pushTime: Firebase.ServerValue.TIMESTAMP});

What the console.log prints is off by ~300ms from what is in fact stored at the path.

解决方案

The behaviour that you are seeing is the expected behaviour.

It occurs because the child_added event is fired locally, using a value for ServerValue.TIMESTAMP that's determined using the local time and the offset of the local clock from the server clock. However, it's not possible to account for the time it will take for the request to arrive at the server - hence the differing timestamps.

If you also listen to the child_changed event, it will fire with a snapshot that includes the correct time once the write is complete.

这篇关于Firebase.ServerValue.TIMESTAMP在即时child_added上返回不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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